I have two collections of type ICollection<MyType>
called c1
and c2
. I'd like to find the set of items that are in c2
that are not in c1
, where the heuristic for equality is the Id
property on MyType
.
What is the quickest way to perform this in C# (3.0)?
Use
Enumerable.Except
and specifically the overload that accepts anIEqualityComparer<MyType>
:Note that this produces the set difference and thus duplicates in
c2
will only appear in the resultingIEnumerable<MyType>
once. Here you need to implementIEqualityComparer<MyType>
as something likeThen, using Linq:
You could also do it using generics and predicates. If you need a sample, let me know.
If using C# 3.0 + Linq:
Loop through complement to get the items.