Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple tables left join using Linq

Tags:

asp.net

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

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

like image 894
pita Avatar asked Jun 06 '12 19:06

pita


1 Answers

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

like image 134
Joanna Derks Avatar answered Dec 06 '22 15:12

Joanna Derks