简体   繁体   中英

Dynamically specifying “include” for Entity Framework queries

I am trying to pass a List<string> to a method that will take it and use it to create a list of include statements to be used in an entity framework query.

For example:

List<string> myIncludes = new List<string>();
myIncludes.Add("myObject.FirstRelatedObject");
myIncludes.Add("myObject.SecondRelatedObject");

I want to use this list to get something like the following, dyamically:

var myQry = objectContext.object.Include(myIncludes[0]).Include(myIncludes[1]);

How would I go about doing this? I am using predicateBuilder to generate the "where" part of the statement, but I don't think that's the same thing as the "Include" portion.

You can try something like this (where predicate is your PredicateBuilder):

var includes = new List<string>() { "Include1, Include2" };

var query = this.Context.Campos;

foreach(var s in includes)
    query.Include(s);

var result = query.Where(predicate.Expand());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM