简体   繁体   English

外部加入Linq或EF

[英]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 我想得到一个外连接,以便我得到1,2,3,4,5

I am using following Linq command 我正在使用以下Linq命令

   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 输出我得到我1,2,3,4失踪5.我怎么能得到它给我newList 1,2,3,4,5请帮助

There is no direct alternative to OUTER JOIN in LINQ. 在LINQ中没有直接替代OUTER JOIN 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 . 在查询中你只写了在T2中也存在的i ,因为on i.id1 equals d.id2

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

You could use Union like: 你可以像使用Union一样:

var result = T1.Union(T2); var result = T1.Union(T2);

You can refer to this C# linq union question 你可以参考这个C#linq union问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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