How to design a query when the table name is a parameter in Linq To Entities

dynamic-linq dynamic-sql entity-framework

Question

Dynamic queries are not dynamic enough. I have seen solutions like this but still I have to indicate which table to use as the basis:

var query = db.Customers.Where("...").OrderBy("...").Select("...");

I want to create a simple query tool where the user will select from available tables using a drop-down list. As the result, I want to show first few records. Therefore, I need to change the table too! That is, I need something like this:

string selectedTable = "Customers";
var [tableName] = SomeTypecastingOperations(selectedTable);
var query = db.[tableName].Where("...").OrderBy("...").Select("...");

Is EF dynamic enough to handle this?

1
0
3/14/2012 1:15:56 PM

Accepted Answer

Linq-to-entities doesn't support that. You can achieve that with Entity SQL or some ugly code which will have conditional logic for every set you want to query (like a big switch for table names).

0
3/14/2012 1:13:57 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow