【问题标题】:Left outer join in LinqLinq中的左外连接
【发布时间】:2011-08-04 20:16:41
【问题描述】:

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

我有 2 个列表:左侧和右侧。我需要选择左边所有不在右边的元素。

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

IEnumerable result = // Have no idea

我该怎么做?

【问题讨论】:

    标签: linq outer-join


    【解决方案1】:

    这听起来根本不像是一个连接...听起来像:

    var result = left.Except(right);
    

    【讨论】:

    • 貌似就是这样,SQL太习惯了
    【解决方案2】:

    这是我找到的解决方案。

    查找所有未购买的客户:

    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
       }
    

    【讨论】:

      猜你喜欢
      • 2011-04-27
      • 2011-03-25
      • 2011-08-10
      • 1970-01-01
      相关资源
      最近更新 更多