Query Table Using Table Names or Meta Tags - LINQ

Is there a way to use meta tags or table names to build 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? Trying above gives an error:

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

Thank you, Chris

+4
source share
2 answers

the "long answer" is a lot of unpleasant expression code: you need to create an expression tree for your query from scratch, because the compiler does this only if everything is strongly typed. However, if you just want to get all the lines, just write

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

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

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

+3
source

Have you included a reference to the System.Data.Linq namespace? If this is not enough, you are likely to get this error, since I think LINQ extension methods come from this.

+1
source

Source: https://habr.com/ru/post/1338933/


All Articles