LINQ dynamic from clause

c# dynamic-linq entity-framework linq

Question

I use dynamic LINQ for creating queries in runtime. Here is an example:

var result = dbContext.Table1
.Where(x => x.Field1 == "SomeValue")
.Select(x => new { x.Field1, x.Field2 });

Here I can dynamically change where clause and select clause, but is there any way for dynamic from clause?

If I want to change dbContext.Table1 with some other table in runtime? Or I must use SQL for that?

EDIT: About duplicate question suggestion: It is the same question, but there isn't right answer in that question. The answer provided there is not generic enough, I would have to put a case statement for each new table...

1
3
10/13/2015 10:14:00 AM

Popular Answer

If you know the type of the entity you need to retrieve, you can use the Set<> method of your db context instead of the Table1 property. For example:

var data = dbContext.Set<EntityType>();
0
10/13/2015 10:18:28 AM


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