Entity framework 实体框架中使用lambda语法的多个左连接

Entity framework 实体框架中使用lambda语法的多个左连接,entity-framework,entity-framework-core,Entity Framework,Entity Framework Core,我有以下三张桌子 课程 Id, SortOrder, CourseName, CourseArea, CourseFor Id, FullName CourseId, StudentId, CollegeId 学生 Id, SortOrder, CourseName, CourseArea, CourseFor Id, FullName CourseId, StudentId, CollegeId 课程学生 Id, SortOrder, CourseName, CourseArea,

我有以下三张桌子

课程

Id, SortOrder, CourseName, CourseArea, CourseFor
Id, FullName
CourseId, StudentId, CollegeId
学生

Id, SortOrder, CourseName, CourseArea, CourseFor
Id, FullName
CourseId, StudentId, CollegeId
课程学生

Id, SortOrder, CourseName, CourseArea, CourseFor
Id, FullName
CourseId, StudentId, CollegeId
要求:

在“125”学院为“外国”学生提供“医学”区域的所有课程学生。包括课程,即使没有学生注册

工作SQL查询:

SELECT cr.Id, cr.CourseName, st.FullName
FROM dbo.Courses cr
LEFT JOIN dbo.CourseStudents cst ON cr.Id = cst.CourseId 
                                 AND cst.CollegeId = 125
LEFT JOIN dbo.Students st ON cst.StudentId = st.Id
WHERE 
    cr.CourseArea = 'Medical'
    AND cr.CourseFor = 'Foreigner'
ORDER BY 
    cr.SortOrder, st.FullName
谁能帮我学习lambda语法(我试过
GroupJoin
)?虽然我要寻找的是lambda语法,但查询语法也是很好的

更新:我非常接近,但仍然没有完成

    context.Courses
        .GroupJoin(context.CourseStudents,
            x => new { x.Id, CollegeId NOT IN COURSES TABLE :( },
            y => new { Id = y.CourseId, y.CollegeId=125 },
            (x, y) => new { Courses = x, CourseStudents = y })
        .SelectMany(x => x.CourseStudents.DefaultIfEmpty(),
            (x, y) => new { x.Courses, CourseStudents = y })
        .GroupJoin(context.Students,
            x => x.CourseStudents.StudentId,
            y => y.Id,
            (x, y) => new { CoursesCourseStudents = x, Students = y }
        )
        .SelectMany(x => x.Students.DefaultIfEmpty(),
        (x, y) => new { x = x.CoursesCourseStudents, Students = y })
        .Select(x => new
        {
            x.x.Courses.Id,
            x.x.Courses.CourseName,
            x.Students.FullName,
            x.x.CourseStudents.CollegeId,
            x.x.Courses.CourseFor,
            x.x.Courses.CourseArea,
            x.x.Courses.SortOrder
        })
        .Where(x => x.CourseFor == "Foreigner" && x.CourseArea == "Medical")
        .OrderBy(x => x.SortOrder)
        .ToList();

解决方案:我通过执行以下操作使其正常工作。参见第3行和第4行

context.Courses
    .GroupJoin(context.CourseStudents,
        x => new { x.Id, CollegeId=125 },
        y => new { Id = y.CourseId, y.CollegeId },
        (x, y) => new { Courses = x, CourseStudents = y })
    .SelectMany(x => x.CourseStudents.DefaultIfEmpty(),
        (x, y) => new { x.Courses, CourseStudents = y })
    .GroupJoin(context.Students,
        x => x.CourseStudents.StudentId,
        y => y.Id,
        (x, y) => new { CoursesCourseStudents = x, Students = y }
    )
    .SelectMany(x => x.Students.DefaultIfEmpty(),
    (x, y) => new { x = x.CoursesCourseStudents, Students = y })
    .Select(x => new
    {
        x.x.Courses.Id,
        x.x.Courses.CourseName,
        x.Students.FullName,
        x.x.CourseStudents.CollegeId,
        x.x.Courses.CourseFor,
        x.x.Courses.CourseArea,
        x.x.Courses.SortOrder
    })
    .Where(x => x.CourseFor == "Foreigner" && x.CourseArea == "Medical")
    .OrderBy(x => x.SortOrder)
    .ToList();

在EF中,我们几乎从不使用手动连接,而是使用导航属性。使用导航属性时,查询应该是简单的
Select
SelectMany
,需要时使用
Where
。要获得更具体的答案,我们需要相关的实体模型(类和配置),而不是tables.LINQ方法DefaultIfEmpty(),它与SQL的左连接非常相似。我相信英孚有能力做到这一点。我想知道怎么做。还有其他更好的方法,但我想知道如何使用lambda实现这一点。上面的链接没有帮助,但我很感谢你的帮助。