Daniel
Daniel

Reputation: 31579

Left outer join in Linq

I know there is tons of posts on this but they are all about specific problems that I didn't understand what is left, right and anything

I have 2 lists: left and right. I need to select all elements in left that are not in right.

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

IEnumerable result = // Have no idea

How do I do this?

Upvotes: 2

Views: 235

Answers (2)

KeithMacCrimmon
KeithMacCrimmon

Reputation: 219

Here is a solution I found.

Find all customers without purchases:

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
   }

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500755

That doesn't sound like a join at all... that sounds like:

var result = left.Except(right);

Upvotes: 7

Related Questions