How does C-sharp LINQ implement queries like left join and right join in SQL with method syntax?

how does C-sharp LINQ implement queries like left join and right join in SQL with method syntax?

May.21,2022

..


GroupJoin

right join is to write the right table on the left


//inner join
from a in StoreInCities
join b in CityInfo on a.CityId equals b.ID into c
select c

//left join   (right join)
from a in StoreInCities
join b in CityInfo on a.CityId equals b.ID into c
//DefaultIfEmpty()DefaultIfEmpty()inner join
from d in c.DefaultIfEmpty()
select d

//left join
StoreInCities
   .GroupJoin (
      CityInfo, 
      a => a.CityId, 
      b => b.ID, 
      (a, c) => 
         new  
         {
            a = a, 
            c = c
         }
   )
   .SelectMany (
      temp0 => temp0.c.DefaultIfEmpty (), 
      (temp0, d) => d
   )


//sqllinglinglinqpad
Menu