【发布时间】:2012-06-05 01:32:33
【问题描述】:
Include() 方法对于对象列表非常有效。但是,如果我需要深入两层怎么办?例如,下面的方法将返回具有此处显示的包含属性的 ApplicationServers。但是,ApplicationsWithOverrideGroup 是另一个包含其他复杂对象的容器。我也可以在该属性上执行 Include() 吗?或者我怎样才能让该属性完全加载?
就目前而言,这种方法:
public IEnumerable<ApplicationServer> GetAll()
{
return this.Database.ApplicationServers
.Include(x => x.ApplicationsWithOverrideGroup)
.Include(x => x.ApplicationWithGroupToForceInstallList)
.Include(x => x.CustomVariableGroups)
.ToList();
}
将仅填充 Enabled 属性(下),而不是 Application 或 CustomVariableGroup 属性(下)。我该如何做到这一点?
public class ApplicationWithOverrideVariableGroup : EntityBase
{
public bool Enabled { get; set; }
public Application Application { get; set; }
public CustomVariableGroup CustomVariableGroup { get; set; }
}
【问题讨论】:
-
嗨,为什么我在尝试这个时得到一个异常
Expression must be a member expression:要包含一个集合,然后是一个向下一级的集合:query.Include(e => e.Level1Collection.Select(l1 => l1.Level2Collection))。 -
@BobHorn,我有同样的问题。在我的情况下,嵌套深入到多层,我设法做了一个包括你指出的事情。在生成的 SQL 中,我可以看到所有列都以不同的别名返回,如 c1、c2 之类的。我的问题是,我如何从我的所有包含中形成一个嵌套的 DTO 集合:(.. 可能你可以自己拿上面的例子,因为我们正在返回所有没有任何自定义 DTO 的列(它本身就是 DTO 的集合)
标签: c# entity-framework eager-loading