Chris Lucian Chris Lucian - 1 year ago 78 Question

Query Table using Table Names or MetaTables - LINQ

Is there a way to use Meta Tales or Table names to Construct a dynamic query in LINQ?

foreach (var metaTable in db.Mapping.GetTables())
var queryType = metaTable.RowType.Type;

var test = from q in db.GetTable(queryType)
select q;

Is there a way to do something like this? The attempt above yields the error:

Could not find an implementation of the query pattern for source type 'System.Data.Linq.ITable'. 'Select' not found. Consider explicitly specifying the type of the range variable 'q'.


usr usr
Answer Source

The "long answer" is a lot of nasty expression code: You have to build up the expression tree for your query from scratch because the compiler only does it if everything is strongly typed. However if you just want to get all rows, just write

var test = db.GetTable(queryType).Cast<object>();

because the ITable interface is already IEnumerable. Now you still need to make sense of a sequence of untyped objects.

Can you tell us what you want to do and why?