Entity Framework中使用Lambda语法进行多个左连接

4

我有以下3个表格

课程

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();

1
在 EF 中,我们几乎从不使用手动连接,而是使用导航属性。有了导航属性,查询应该简单明了,只需带上必要的 WhereSelect 或者 SelectMany。为了得到更具体的答案,我们需要相关的实体模型(类和配置),而不是表格。 - Ivan Stoev
LINQ方法DefaultIfEmpty()与SQL的Left Join非常相似。请参考https://www.c-sharpcorner.com/UploadFile/97fc7a/linq-method-defaultifempty/。 - Amin Golmahalle
我相信 EF 能够做到。我想知道如何做到。可能有其他更好的方法,但我想知道如何使用 lambda 实现。上面的链接没有帮助,但我感谢您的帮助。 - Rajeev Menon
1个回答

3
解决方案:我通过以下方法使其工作。请参见第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();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接