多个表的左连接使用LINQ [英] Multiple tables left join using Linq

查看:575
本文介绍了多个表的左连接使用LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know the Linq's left join is similar like this:

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   
      };

那么如何对多个连接?这里是我的code

then how about the multiple join? here is my code

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,
                         };

每个连接表可以有空领域。可以在任何帮助我,谢谢

each joined table could have "null" field. can any help me, thanks

推荐答案

多参加看起来颇为相似 - 它变得相当冗长,但我会试试这个。
您可能需要在最后其中,太行一些空的检查。

Multi join should look quite similar - it gets quite verbose, but I would give this a try. You might need some null checking in the final where line too.

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,
                         };

有似乎是做外的另一种方式加入,以及,但没有有得测试它,我甚至不能确定是否有可能在这种情况下使用它 - 检查出的答案对这个职位,如果你兴趣:
在LINQ

There seems to be another way of doing outer joins as well but without having something to test it on I'm not even sure if it's possible to use it in this case - check out the answer on this post if you're interested: outer join in linq

这篇关于多个表的左连接使用LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆