c# - Entity Framework 包含左连接这可能吗?

标签 c# entity-framework left-join

我有以下表格

  1. 类(ClassID,ClassName)
  2. StudentClass (StudentID,ClassID)
  3. 学生(StudentID、StudentName 等)
  4. 学生描述。 (StudentDescriptionID,StudentID,StudentDescription)

我要检索student==1的所有信息

在 sql 中,我会做类似下面的事情并获取有关学生的所有信息。

 select * from Student s
 join StudentClass sc on s.StudentID=sc.StudentID
 join ClassRoom c on sc.ClassID=c.ClassID
 left join StudentDescription sd on s.StudentID=sd.StudentID
 where s.StudentID=14

现在是我的问题。使用 EF4 我做了类似的事情,但无法使其工作。 你也可以做一个包含和一个左连接

尝试 1

private static StudentDto LoadStudent(int studentId)
    {
        StudentDto studentDto = null;
        using (var ctx = new TrainingContext())
        {
            var query = ctx.Students
                .Include("ClassRooms")
                .Include("StudentDescriptions")
                .Where(x=>x.StudentID==studentId)
                .SingleOrDefault();

            studentDto = new StudentDto();
            studentDto.StudentId = query.StudentID;
            studentDto.StudentName = query.StudentName;
            studentDto.StudentDescription = ??

        }

        return studentDto;
    }

再次尝试2不完整且错误

using (var ctx = new TrainingContext())
         {
             var query = (from s in ctx.Students
                             .Include("ClassRooms")
                         join sd in ctx.StudentDescriptions on s.StudentID equals sd.StudentID into g
                         from stuDesc in g.DefaultIfEmpty()
                         select new
                                    {
                                        Name=s.StudentName,
                                        StudentId=s.StudentID,

         }).SingleOrDefault();

如您所见,我不知道我在这里做什么。 如何将该 Sql 转换为 EF 查询?

最佳答案

是的,这是可能的。

首先,.Include 使用您通过的导航属性 进行左外连接。

这就是在 StudentStudentDescription 之间显式执行 LEFT JOIN 的方式:

var query = from s in ctx.Students
            from sd in s.StudentDescriptions.DefaultIfEmpty()
            select new { StudentName = s.Name, StudentDescription = sd.Description };

如您所见,它根据 StudentsStudentDescriptions 之间的实体关联执行 JOIN。在您的 EF 模型中,您的 Student 实体上应该有一个名为 StudentDescriptions 的导航属性。上面的代码只是简单地使用它来执行连接,如果为空则默认。

代码与.Include基本相同。

请不要混淆 LEFT JOINLEFT OUTER JOIN。

它们是同一件事。

“OUTER”关键字是可选的,我相信它是为了 ANSI-92 兼容性而存在的。

只是 .Include 您在查询中需要的一切:

using (var ctx = new TrainingContext())
        {
            studentDo = ctx.Students
                .Include("ClassRooms")
                .Include("StudentDescriptions")
                .Where(x=>x.StudentID==studentId)
                .Select(x => new StudentDto
                        {
                            StudentId = x.StudentId,
                            StudentName = x.StudentName
                            StudentDescription = x.StudentDescription.Description
                        })
                .SingleOrDefault();
        }

基本上,确保所有 FK 都表示为模型上的导航属性,如果是这样,则不需要进行任何连接。您需要的任何关系都可以通过 .Include 完成。

关于c# - Entity Framework 包含左连接这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4290802/

相关文章:

c# - 搭建新表并保留现有表?

c# - 获取 "cannot insert NULL into ("架构"."table"."column") 当列不为空时

mysql - 在 php 中使用内部连接与 mysql lat long 查询

MySQL JOIN 2 依赖于第一个子查询的子查询

具有多个连接、计数和左连接的 SQL 到 LINQ

c# - 在我使用 linq 键入时运行任务(如果仍在运行,则取消之前的任务)

c# - 如何使用索引器访问枚举项并将数组字符串分配给它以进行显示?

c# 填充 datagridview 非常慢

c# - 远程 COM 服务器实例化

c# - 如何让visual studio在做集成测试时运行一次TestInitialize?