【问题标题】:Linq join with an inner collectionLinq 加入内部集合
【发布时间】:2010-03-23 06:19:55
【问题描述】:

我正在尝试对 2 个集合进行 LINQ to Object 查询

  1. Customer.Orders
  2. Branches.Pending.Orders(集合中的集合)

我想输出每个尚未交付客户订单的分支。

var match = from order in customer.Orders 
    join branch in Branches 
    on order equals branch.Pending.Orders 
    select branch;

这不起作用,我得到: join 子句中的表达式之一的类型不正确。调用“GroupJoin”时类型推断失败。

从我的搜索来看,我认为这是因为 Order 或 Orders 集合没有实现 equals。

如果这个查询有效,它仍然是错误的,因为如果客户的订单和挂单完全匹配,它将返回一个分支。如果任何订单匹配,我想要一个结果。

我正在学习 Linq,并正在寻找解决此类问题的方法,而不是解决方案本身。

我会像这样在 SQL 中做到这一点;

SELECT b.branch_name from Customers c, Branches b, Orders o
WHERE c.customer_id = o.customer_id
  AND o.branch_id = b.branch_id
  AND c.customer_id = 'my customer'
  AND o.order_status = 'pending'

【问题讨论】:

    标签: linq join linq-to-objects


    【解决方案1】:

    看看你的 Linq,你想要这样的东西

    var match = 
       from o in customer.Orders 
       from b in Branches 
       where b.Pending.Orders.FirstOrDefault(p => o.order_id == p.order_id) != null
       select b;
    

    【讨论】:

    • 解决方案 1 更接近我想要的。但是我在“在 b.Pending.Orders 中加入 p”位置得到“名称 'b' 在当前上下文中不存在”
    • 抱歉,在没有编译器的情况下工作 - 试试这个编辑
    • 谢谢,这似乎有效。肯定会更新我的测试用例。
    猜你喜欢
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    相关资源
    最近更新 更多