Linq的左外连接

时间:2011-04-13 15:38:27

标签: linq outer-join

我知道这里有很多帖子,但它们都是关于我不明白什么是左,右和任何事情的具体问题

我有两个列表:左和右。我需要选择左边不正确的所有元素。

List<T> left = GetLeft();
List<T> right = GetRight();

IEnumerable result = // Have no idea

我该怎么做?

2 个答案:

答案 0 :(得分:7)

这听起来不像是一个联接......听起来像是:

var result = left.Except(right);

答案 1 :(得分:1)

这是我找到的解决方案。

查找所有未购买的客户:

SQL:

   Select c.Name from Customers c             
   Left Outer Join Purchases p on c.customerid=p.customerid 
   where p.price is null

LINQ:

   from c in Customers
   join p in Purchases on c.customerid=p.customerid into custPurchases
   from cp in custPurchases.DefaultIfEmpty()
   where cp==null
   select new
   {
   cc.Name
   }
相关问题