【问题标题】:LINQ To Entity - Unique Left Outer Join ScenerioLINQ To Entity - 唯一的左外连接方案
【发布时间】:2012-09-08 12:03:03
【问题描述】:

我正在尝试用 Linq to Entity 语法编写以下 Left Outer Join 场景,但我终其一生都无法弄清楚如何实现它... 这是有效的 SQL strong> 我正在努力最终实现的目标:

选择 *
FROM 学生 左加入 ParentStudents ps ON ps.StudentId = s.StudentId AND ps.ParentId = '6D279F72-2623-459F-B701-5C77C52BA52F'

其中 s.TenantId = 3 AND s.FamilyId = '28833312-46eb-4a54-9132-8a7c8037cec5'

以粗体突出显示的部分是我跌倒的地方......我希望学生返回,无论数据库中是否有任何 ParentStudent 记录。

这是我最新的 LINQ to Entity无法运行的代码

    public ICollection<ParentStudentListing> GetParentStudents(Guid FamilyId, Guid ParentId)
    {
        var query = from s in DataContext.Students
                    from ps in s.ParentStudents.DefaultIfEmpty()
                    where s.TenantId == CurrentUser.TenantId && s.FamilyId == FamilyId && ps.ParentId == ParentId
                    select new ParentStudentListing { StudentId = s.StudentId, FirstName = s.FirstName, MiddleName = s.MiddleName, LastName = s.LastName, RelationshipId = ps.RelationshipId, ParentStudentId = ps.ParentStudentId, isAllowedToPickUp = ps.isAllowedToPickUp, isEmergency = ps.isEmergency, isLiveIn = ps.isLiveIn, ParentId = ps.ParentId };

        return query.ToList();
    }

除非数据库中有非预期结果的 ParentStudent 记录,否则此代码不会带回学生。无论是否有 ParentStudent 记录,我都想带回学生,但是如果有 ParentStudent 记录,我希望那些与学生记录一起加入...

谢谢!

【问题讨论】:

    标签: entity-framework linq-to-entities left-join


    【解决方案1】:

    这是我第一次尝试 LINQ 连接:

    var query = from s in DataContext.Students
                join ps in s.ParentStudents on ps.ParentId equals s.ParentId into ps
                from ps in ps.DefaultIfEmpty()
                where s.TenantId == CurrentUser.TenantId && s.FamilyId == FamilyId                
                select new ParentStudentListing { StudentId = s.StudentId, FirstName = s.FirstName, MiddleName = s.MiddleName, LastName = s.LastName, RelationshipId = ps.RelationshipId, ParentStudentId = ps.ParentStudentId, isAllowedToPickUp = ps.isAllowedToPickUp, isEmergency = ps.isEmergency, isLiveIn = ps.isLiveIn, ParentId = ps.ParentId };
    

    引用自here

    【讨论】:

      【解决方案2】:

      感谢 Joe 的额外帮助...。这并没有完全让我到达那里,但我终于让它运行起来了。这是工作代码:

          public ICollection<ParentStudentListing> GetParentStudents(Guid FamilyId, Guid ParentId)
          {
              var query = from s in DataContext.Students
                          join ps in DataContext.ParentStudents
                                on new { s.StudentId, ParentId = ParentId }
                            equals new { ps.StudentId, ps.ParentId } into ps_join
                          from ps in ps_join.DefaultIfEmpty()
                          where s.TenantId == CurrentUser.TenantId && s.FamilyId == FamilyId
                          select new ParentStudentListing { StudentId = s.StudentId, FirstName = s.FirstName, MiddleName = s.MiddleName, LastName = s.LastName, RelationshipId = ps.RelationshipId, ParentStudentId = ps.ParentStudentId, isAllowedToPickUp = ps.isAllowedToPickUp, isEmergency = ps.isEmergency, isLiveIn = ps.isLiveIn, ParentId = ps.ParentId };
      
              return query.ToList();
          }
      

      【讨论】:

      • Joe 你的回答同样有帮助!再次感谢!
      猜你喜欢
      • 2010-09-09
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      相关资源
      最近更新 更多