Linq Outer Join

Posts   
 
    
Val
User
Posts: 37
Joined: 14-Nov-2008
# Posted on: 05-Feb-2009 17:07:30   

How can I write a Linq expression to select all customer without an order? I tried using the outer join expression and I retrieve all the customers instead.

from c in metaData.Customer
join o in metaData.Order on c.CustomerId equals o.CustomerId into orders
                                 from order in orders.DefaultIfEmpty()
                                 where order == null
                                 select c);
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39859
Joined: 17-Aug-2003
# Posted on: 05-Feb-2009 18:42:57   

var q = from c in metaData.Customer where c.Orders.Count()==0 select c;

there are other alternatives, with .Any as well.

also, your query with: var q = from c in metaData.Customer join o in metaData.Order on c.CustomerId equals o.CustomerId into orders from order in orders.DefaultIfEmpty() where order.OrderId == null select c);

should work too (although I haven't tested that last query).

Frans Bouma | Lead developer LLBLGen Pro