2

I found this code at LINQ - Full Outer Join:

var leftOuterJoin = from first in firstNames
                    join last in lastNames
                    on first.ID equals last.ID
                    into temp
                    from last in temp.DefaultIfEmpty(new { first.ID, Name = default(string) })
                    select new
                    {
                        first.ID,
                        FirstName = first.Name,
                        LastName = last.Name,
                    };
var rightOuterJoin = from last in lastNames
                     join first in firstNames
                     on last.ID equals first.ID
                     into temp
                     from first in temp.DefaultIfEmpty(new { last.ID, Name = default(string) })
                     select new
                     {
                         last.ID,
                         FirstName = first.Name,
                         LastName = last.Name,
                     };

The answer containing this code got a lot of upvotes but I am observing something wrong here. In left outer join it uses into and from with second entity, in right outer join it does the same. So, both implementations seem the same to me. Can you tell me the difference between left outer join and right outer join? Thanks in advance.

EDIT :

I think right outer join should be like this :

var rightOuterJoin = from first in firstNames
                    join last in lastNames
                    on first.ID equals last.ID
                    into temp
                    from first in temp.DefaultIfEmpty(new { last.ID, Name = default(string) })
                   select new
                     {
                         last.ID,
                         FirstName = first.Name,
                         LastName = last.Name,
                     };
Community
  • 1
  • 1
jason
  • 6,962
  • 36
  • 117
  • 198
  • It does the same in this website: http://www.dotnetfunda.com/codes/show/1849/how-to-use-left-right-outer-join-in-linq I really can't see a difference :( – jason May 14 '17 at 16:17
  • 1
    I don't understand the question. The way you turn a left something into a right something is you swap the left and right things. Which is what has been done here. Why do you think this is wrong? – Eric Lippert May 14 '17 at 16:27
  • `temp` variable is of type `IEnumerable` in left join, but is of type `IEnumerable` in right one, so there is a difference. – Evk May 14 '17 at 16:29
  • @EricLippert If I change left join to the code in the edit, would it work? – jason May 14 '17 at 16:35
  • @EricLippert what I can't understand is why do we switch the order? – jason May 14 '17 at 16:41

1 Answers1

1

Here's a visual explanation of the different types of joins.

Visual Representation of Joins

The major difference is that LEFT will keep all records from the 1st table (or left table), while RIGHT will keep all records from the 2nd table.

OUTER returns rows even if NULL values are found in the record.

Asjad Athick
  • 143
  • 3
  • 10
  • Yeah I know this diagram. But this doesn't answer my question. What is different in **implementation**? Is the above code correct, if yes how? If no how? – jason May 14 '17 at 16:11