动态指定实体框架查询的"包含"

Ama*_*son 1 c# entity-framework predicate

我试图将List<string>一个方法传递给它,并使用它来创建一个在实体框架查询中使用的include语句列表.

例如:

List<string> myIncludes = new List<string>();
myIncludes.Add("myObject.FirstRelatedObject");
myIncludes.Add("myObject.SecondRelatedObject");
Run Code Online (Sandbox Code Playgroud)

我想使用这个列表来获得类似下面的内容:dyamically:

var myQry = objectContext.object.Include(myIncludes[0]).Include(myIncludes[1]);
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?我使用predicateBuilder来生成语句的"where"部分,但我认为这与"Include"部分不同.

mar*_*osz 5

你可以试试这样的东西(其中谓词是你的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());
Run Code Online (Sandbox Code Playgroud)