J. Davidson
J. Davidson

Reputation: 3307

Outer Join in Linq or EF

I have two tables

T1     T2
-------------
id1    id2
-----------
1      3
2      5
3
4

I want to get a outer join so that I get 1,2,3,4,5

I am using following Linq command

   var newList = (from i in T1
                   join d in T2
                   on i.id1 equals d.id2 into output
                   from j in output.DefaultIfEmpty()
                   select new {i.id}); 

The out put I get i 1,2,3,4 missing 5. How can I get it to give me newList 1,2,3,4,5 Help Please

Upvotes: 0

Views: 272

Answers (2)

Dietz
Dietz

Reputation: 578

You could use Union like:

var result = T1.Union(T2);

You can refer to this C# linq union question

Upvotes: 0

Jan P.
Jan P.

Reputation: 3297

There is no direct alternative to OUTER JOIN in LINQ. You have to solve it like this:

Within query you wrote only the i's that also exists in T2 because of the on i.id1 equals d.id2.

var result = T1.Select(item => item.id1).Union(T2.Select(item => item.id2));

Upvotes: 2

Related Questions