Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Table (ObjectSet) based on table name (Entity Framework)

My goal is to use entity frameworks (perhaps reflection), to create functions like this:

public List<MyRows> getTableByName(string tableName)
{
   ObjectQuery objectQuery = (ObjectQuery)getObjectSet(tableName, m_context); // How do you do this part???
   List<MyRows> rows = new List<MyRows>();
   SortByColumn(objectQuery); // I can do this myself.
   MyWhereClauses(objectQuery); // I can do this myself.
   rows = ConvertToMyRows(objectQuery.ToList()); // I can do this myself.
}

The class "MyRows" is invented by me so I can add more properties. The "ConvertToMyRows", "MyWhereClauses", and "getObjectset" are also made-up functions.

Is this sort of thing possible in Entity Framework?

How do you do a sort of "getObjectSet" type function based on Template T or string of the table name?

I'm tired of writing Data Access files that have specific functions just to generate a list of items.

SOLVED:

public IQueryable<T> GetTable<T>() where T : class
{
 var table = m_entities.CreateObjectSet<T>();
 return table;
}

public string MyData<T>() where T: class { var table = GetTable<T>(); //order here }
like image 216
Dexter Avatar asked Mar 21 '26 10:03

Dexter


1 Answers

public IQueryable<T> GetTable<T>() where T : class
{
 var table = m_entities.CreateObjectSet<T>();
 return table;
}

public string MyData<T>() where T: class { 

  var table = GetTable<T>(); //order here

 }
like image 154
Dexter Avatar answered Mar 24 '26 00:03

Dexter