views:

214

answers:

1

Hi,

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'
A: 

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;
pdr
Solution 1 is closer to what I want. But I get "The name 'b' does not exist in the current context" at the "join p in b.Pending.Orders" location
bronze
Sorry, working without a compiler handy - try this edit instead
pdr
Thanks, that seems to work. Will update my test cases to be sure.
bronze