开发者

Linq join with an inner collection

开发者 https://www.devze.com 2022-12-23 16:11 出处:网络
I am trying a LINQ to Object query on 2 collections Customer.Orders Branches.Pending.Orders (Collection within a collection)

I am trying a LINQ to Object query on 2 collections

  1. Customer.Orders
  2. Branches.Pending.Orders (Collection within a collection)

I want to output each branch which is yet to deliver any order of the customer.

var match = from order in customer.Orders 
    join branch in Branches 
    on order equals branch开发者_运维问答.Pending.Orders 
    select branch;

This does not work, I get : The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'.

From my search, I think this is because Order or collection of Orders does not implement equals.

If this query worked, it will still be wrong, as it will return a branch if the customer's and pending orders match exactly. I want a result if any of the order matches.

I am learning Linq, and looking for a approach to address such issues, rather than the solution itself.

I would have done this in SQL like this;

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'


Looking at your Linq, you want something like this

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

精彩评论

暂无评论...
验证码 换一张
取 消