KiRa
KiRa

Reputation: 924

LINQ Right and Left Joins

I am practicing LINQ in LINQ Pad with Right, Left and Inner Joins I notice something

(from a in Employees join b in Persons
     on a.PersonId equals b.PersonId into x
     from c in x.DefaultIfEmpty()
select  new {
        a.EmployeeId,
        c.PersonId,
        c.CompleteName
}).Take(50).OrderByDescending(m => m.EmployeeId)

and

(from a in Persons join b in  Employees
    on a.PersonId equals b.PersonId
select new {
    b.EmployeeId,
    b.PersonId,
    a.CompleteName
 }).Take(50).OrderByDescending(m => m.EmployeeId)

have the same result.

(from a in Persons join b in  Employees
    on a.PersonId equals b.PersonId into x
    from c in x.DefaultIfEmpty()
select 
     new {
        c.EmployeeId,
        c.PersonId,
        a.CompleteName
}).Take(50).OrderByDescending(m => m.EmployeeId)

I search for right and left joins in linq but my problem is where is the right or left joins in my examples?.

Upvotes: 0

Views: 112

Answers (2)

teo van kot
teo van kot

Reputation: 12491

By default all joins in LinQ are INNER (Nor right or left).

Your 1st and 3rd query are LEFT OUTHER JOIN becouse of x.DefaultIfEmpty() line. 2nd - INNER JOIN.

1st and 2nd query have same result maby becouse of .Take(50), on your 50 rows could be there is no difference between INNER and OUTHER.

Upvotes: 3

Viplock
Viplock

Reputation: 3319

I don't see anything wrong in your queries , those looks fine . To see if there is any changes in inner join and Left join or right join, You have to have data according to that.

If there will be some Persons which are not employees , will be seen in your first query , and for that yo have to check entire data , first 50 may not tell you . and same goes for opposite.

Upvotes: 1

Related Questions