Entity framework LINQ到实体-唯一左外部联接场景
我正试图用Linq to Entity语法编写以下左外连接场景,但我一辈子都搞不清楚如何实现它以下是我试图最终实现的工作SQL: 选择*Entity framework LINQ到实体-唯一左外部联接场景,entity-framework,linq-to-entities,left-join,Entity Framework,Linq To Entities,Left Join,我正试图用Linq to Entity语法编写以下左外连接场景,但我一辈子都搞不清楚如何实现它以下是我试图最终实现的工作SQL: 选择* 来自学生的 在ps.StudentId=s.StudentId和ps.ParentId='6D279F72-2623-459F-B701-5C77C52BA52F'上左键连接家长学生ps 其中s.TenantId=3,s.FamilyId='2883312-46eb-4a54-9132-8a7c8037cec5' 粗体突出显示的部分是我摔倒的地方。。。我希望学
来自学生的 在ps.StudentId=s.StudentId和ps.ParentId='6D279F72-2623-459F-B701-5C77C52BA52F'上左键连接家长学生ps 其中s.TenantId=3,s.FamilyId='2883312-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();
}
公共ICollection GetParentStudents(Guid FamilyId,Guid ParentId)
{
var query=来自DataContext.Students中的s
来自s.ParentStudents.DefaultIfEmpty()中的ps
其中s.TenantId==CurrentUser.TenantId&&s.FamilyId==FamilyId&&ps.ParentId==ParentId
选择新的ParentStudentListing{StudentId=s.StudentId,FirstName=s.FirstName,MiddleName=s.MiddleName,LastName=s.LastName,RelationshipId=ps.RelationshipId,ParentStudentId=ps.ParentStudentId,isAllowedToPickUp=ps.isEmergency=ps.isEmergency,isLiveIn=ps.isLiveIn,ParentId=ps.ParentId};
返回query.ToList();
}
除非数据库中存在非预期结果的ParentStudent记录,否则此代码不会返回学生。我想把学生带回来,不管是否有家长学生记录,但如果有家长学生记录,我希望这些记录与学生记录结合起来
谢谢大家! 这是我第一次尝试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 };
引用自。感谢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();
}
公共ICollection GetParentStudents(Guid FamilyId,Guid ParentId)
{
var query=来自DataContext.Students中的s
在DataContext.ParentStudents中加入ps
在新的{s.StudentId,ParentId=ParentId}
等于新的{ps.StudentId,ps.ParentId}到ps_join
从ps_join.DefaultIfEmpty()中的ps
其中s.TenantId==CurrentUser.TenantId&&s.FamilyId==FamilyId
选择新的ParentStudentListing{StudentId=s.StudentId,FirstName=s.FirstName,MiddleName=s.MiddleName,LastName=s.LastName,RelationshipId=ps.RelationshipId,ParentStudentId=ps.ParentStudentId,isAllowedToPickUp=ps.isEmergency=ps.isEmergency,isLiveIn=ps.isLiveIn,ParentId=ps.ParentId};
返回query.ToList();
}
Joe你的回答同样有用!再次感谢!