gpt4 book ai didi

asp.net - 剩下的多个表使用Linq连接

转载 作者:行者123 更新时间:2023-12-04 05:19:42 25 4
gpt4 key购买 nike

我知道Linq的左连接类似这样:

var q=from e in db.Employes    
join o in db.Orders on e equals o.Emoloyee into ords  
from on in ords.DefautIfEmpty()
select new
{      
e.FirstName,     
e.LastName   
  };

那么多重联接呢?这是我的代码
var personalInfoQuery = from t in crnnsupContext.Tombstones
join p in crnnsupContext.ProvStates on t.ProvinceState equals p.ProvinceStateID
join n in crnnsupContext.NursingSchools on t.NursingSchool equals n.SchoolID
join i in crnnsupContext.InitialEducations on t.InitialEducation equals SqlFunctions.StringConvert((double)i.InitalEducationID, 1)
join g in crnnsupContext.tbl_GraduatedProvCountry on t.GradPovCountry equals g.id
where t.RegNumber == _username
select new CPersonalInfo
{
ProvState = p,
Tombstone = t,
NursingSchool = n,
InitialEducation = i,
GraduatedProvCountry = g,
};

每个联接表都可以具有“null”字段。有什么可以帮助我的,谢谢

最佳答案

多连接看起来应该很相似-变得很冗长,但我会尝试一下。
您可能还需要在最后的where行中进行一些null检查。

var personalInfoQuery = from t in crnnsupContext.Tombstones
join p in crnnsupContext.ProvStates on t.ProvinceState equals p.ProvinceStateID into group1
from g1 ini group1.DefaultIfEmpty()
join n in crnnsupContext.NursingSchools on g1.NursingSchool equals n.SchoolID into group2
from g2 in group2.DefaultIfEmpty()
join i in crnnsupContext.InitialEducations on g2.InitialEducation equals SqlFunctions.StringConvert((double)i.InitalEducationID, 1) into group3
from g3 in group3.DefaultIfEmpty()
join g in crnnsupContext.tbl_GraduatedProvCountry on g3.GradPovCountry equals g.id into group4
from g4 in group4.DefaultIfEmpty()
where g4 == null || g4.RegNumber == _username
select new CPersonalInfo
{
ProvState = p,
Tombstone = t,
NursingSchool = n,
InitialEducation = i,
GraduatedProvCountry = g,
};

似乎还有另一种进行外部联接的方法,但是没有进行任何测试的方法,我什至不确定在这种情况下是否可以使用它-如果您有兴趣,请查看这篇文章的答案:
outer join in linq

关于asp.net - 剩下的多个表使用Linq连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10920653/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com