Janvier M Janvier M - 1 year ago 86
C# Question

Creating List of objects from Dynamic Tables

I am relatively new to .NET from 12 yrs on PHP. I created a MVC project. In my project, I created a class in the Models that has a Public method that creates a LIST of items that can be manipulated as needed anywhere in the project as follows:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace MYPROJECT.Models
public class Common
public List<Tablex> CommonList(int limit )
var theList = db.Tablex.Take(limit).ToList();

return theList;

To call the list in a view for instance, I do something like this and it works fine:

var thecommon = new MYPROJECT.Models.Common();
var sessions = thecommon.CommonList(45);
@foreach (var item in sessions)
@Html.Raw("<div class="someclass">"+item.column1+" >> "+item.column2+"</div>");


I would like to make Tablex so that I can call any table as needed, and adjust the columns i,j as needed in my views or other controllers.

Any idea how to proceed?

PS: As you can possibly tell, this list is populated by inputs in the database table

Answer Source

I solved the issue by running a Raw query. I can pass the table or the columns dynamically and then create a list object from the result.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download