I need to get table data from table name from Linq DataContext.
Instead of this
var results = db.Authors;
I need to do something like this.
string tableName = "Authors";
var results = db[tableName];
It could be any table name that is available in DataContext.
Given DataContext context
and string tableName
, you can just say:
var table = (ITable)context.GetType()
.GetProperty(tableName)
.GetValue(context, null);
I am not sure if passing strings is an elegant solution. I would rather send the Type of entity as an argument to a method. Something on these lines :
var table = _dataCont.GetTable(typeof(Customer));
Here is the MSDN documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With