LINQ - Find all items in one list that aren't in another list
Solution 1:
Try using .Except
extension method (docs):
var result = list1.Except(list2);
will give you all items in list1
that are not in list2
.
IMPORTANT: Even though there's a link provided to MSDN docs for the method, I'll point this out here: Except
only works out of the box for collections of primitive types, for POCOs/objects you need to implement IEquatable on that object.
Solution 2:
Try this:
var List2 = OriginalList.Where(item => !List1.Any(item2 => item2.ID == item.ID));
Solution 3:
The easiest way is to use the Except
method.
var deletedItems = list1.Except(joinItems);
This will return the set of items in list1
that's not contained in joinItems