如何使用C#动态添加Include到ObjectSet <Entity>?

Mas*_*ian 4 c# entity-framework

我想Include从输入中动态添加s params[].我怎样才能做到这一点?

这是我的代码

IQueryable<Work> query = this.ObjectContext.Works
    .Include("EmployeeSender.Person")
    .Include("EmployeeReceiver.Person")
    .Include("WorkCode")
    .Include("WorkFlowStep.WorkFlowFormState")
    .Include("WorkFlow")
    .Include("WorkRoot.EmployeeSender.Person")
    .Include("WorkParent");
Run Code Online (Sandbox Code Playgroud)

L-F*_*our 6

在循环中,例如:

IQueryable<Work> query = null;  

query = this.ObjectContext.Works;
foreach (var param in params)
{
    query = query.Include(param);
}
var result = query.ToList();
Run Code Online (Sandbox Code Playgroud)

正如Christian Dietz所提到的那样,你可以将它放在一个扩展方法中,以便它可以重复使用.