Multiple tables left join using Linq

后端 未结 1 857
孤街浪徒
孤街浪徒 2021-02-08 20:27

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          


        
1条回答
  •  别那么骄傲
    2021-02-08 20:47

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

    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

    0 讨论(0)
提交回复
热议问题