I have two collections which have property Email
in both collections. I need to get a list of the items in the first list where Email
does not exist in the second list. With SQL I would just use "not in", but I do not know the equivalent in LINQ. How is that done?
So far I have a join, like...
var matches = from item1 in list1
join item2 in list2 on item1.Email equals item2.Email
select new { Email = list1.Email };
But I cannot join since I need the difference and the join would fail. I need some way of using Contains or Exists I believe. I just have not found an example to do that yet.
Example using List of int for simplicity.
I did not test this with LINQ to Entities:
Alternatively:
For anyone who also wants to use a SQL-alike
IN
operator in C#, download this package :It has
In
andNotIn
methods:Even you can use it this way
One could also use
All()
I don't know if this will help you but..
from The NOT IN clause in LINQ to SQL by Marco Russo