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();
}
Run Code Online (Sandbox Code Playgroud)
将仅填充Enabled属性(下方),而不填充Application或CustomVariableGroup属性(如下所示).我该如何实现这一目标?
public class ApplicationWithOverrideVariableGroup : EntityBase
{
public bool Enabled { get; set; }
public Application Application { get; set; }
public CustomVariableGroup CustomVariableGroup { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 1 个测试分配了 1 个测试类型。
我需要加载与 Test 相关的所有 TestType 实体 OR not 和 Schoolclass , Subject 和 Assigned Pupils 到测试 (PupilsTests)
.SelectMany 或 .Select 在运行时失败。
我尝试了 Include 和 ThenInclude 的不同组合,但没有机会通过 PupilsTests 获得测试。
当我开始查询时,我只能通过测试获得 PupilsTests context.Tests
但这导致我只得到分配给测试的测试类型 - 内连接- 但我需要所有测试类型及其测试和他们的学生测试,我希望在一个查询中使用它。
var testtypes = await context.TestTypes
.Include(x => x.Schoolclass)
.Include(x => x.Subject)
.Include(x => x.Tests.SelectMany(z => z.PupilsTests))
.Where(t => t.SchoolyearId == schoolyearId)
.AsNotTracking()
.ToListAsync();
public class TestType
{
public TestType()
{
Tests = new HashSet<Test>();
}
// Props removed for clarity …Run Code Online (Sandbox Code Playgroud)