我有一个测试实体:
public class Test
{
public Test()
{
PupilsTests = new HashSet<PupilTest>();
TestTypeTests = new HashSet<TestTypeTest>();
SchoolclassTests = new HashSet<SchoolclassTest>();
SubjectTests = new HashSet<SubjectTest>();
}
public int Id { get; set; }
public DateTime Date { get; set; }
public int Number { get; set; }
public ISet<PupilTest> PupilsTests { get; set; }
public ISet<TestTypeTest> TestTypeTests { get; set; }
public ISet<SchoolclassTest> SchoolclassTests { get; set; }
public ISet<SubjectTest> SubjectTests { get; set; }
public GradingKey ScoreGradeKey { get; set; }
public Schoolyear Schoolyear { get; set; }
public int SchoolyearId { get; set; }
}
我需要获取由schoolyearId过滤的所有测试实体,包括连接表SchoolclassTests,SubjectTests,TestTypeTests。
但是使用这些联结表我还必须包括他们的主要表 Schoolclass,Subject,TestType 。
这就是我的尝试:
public async Task<IEnumerable<Test>> GetTestsAsync(int schoolyearId)
{
return await context.Tests.Where(t => t.SchoolyearId == schoolyearId)
.Include(t => t.SchoolclassTests)
.Include(t => t.SubjectTests)
.Include(t => t.TestTypeTests)
// How to include all 3 Principal tables? ThenInclude does not workk
// over all the 3...
.ToListAsync();
}
无论我尝试什么组合.Include或ThenInclude我永远不会在一个查询中获得所有3个带有联结表的主表。
我该怎么做?
答案 0 :(得分:0)
使用Select
方法,您可以包含连接表的主要表格:
public async Task<IEnumerable<Test>> GetTestsAsync(int schoolyearId)
{
return await context.Tests.Where(t => t.SchoolyearId == schoolyearId)
.Include(t => t.SchoolclassTests.Select(s => s.Schoolclass))
.Include(t => t.SubjectTests.Select(s => s.Subject))
.Include(t => t.TestTypeTests.Select(t => t.TestType))
.ToListAsync();
}