简体   繁体   中英

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

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

You could use Union like:

var result = T1.Union(T2);

You can refer to this C# linq union question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM