Use Enumerable.Except
and specifically the overload that accepts an IEqualityComparer<MyType>
:
var complement = c2.Except(c1, new MyTypeEqualityComparer());
Note that this produces the set difference and thus duplicates in c2
will only appear in the resulting IEnumerable<MyType>
once. Here you need to implement IEqualityComparer<MyType>
as something like
class MyTypeEqualityComparer : IEqualityComparer<MyType> {
public bool Equals(MyType x, MyType y) {
return x.Id.Equals(y.Id);
}
public int GetHashCode(MyType obj) {
return obj.Id.GetHashCode();
}
}