Add Include Expressions dynamically

c# entity-framework entity-framework-core linq

Question

I am using Entity Framework Core and I have the following:

String expression = "Country;User;User.Country"

This indicates to include Country, User and User.Country in a query:

var q = context.Jobs
          .Include(x => x.Country)
          .Include(x => x.User).ThenInclude(x => x.Country);

I do not know what expression will contain. I just know it will be a list of entities, with or not child entities (ex: User.Country) and I need build the Include expression.

Is there a way to do this?

1
0
6/28/2016 8:49:51 PM

Accepted Answer

there is two way to call include method. one is with expression and the other is with string.

String expression = "Country;User;User.Country"

string includes = expression.split(';');

var q = context.Jobs;

foreach (string include in includes)
    q = q.Include(include);
6
6/28/2016 6:50:23 PM


Related Questions





Related

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