List<ClassA> newlist;
List<ClassA> oldlist;
ClassA has 20 different properties,
I want to
ClassA as the values will not be relavantLinq Except or Intersect doesnt seem to support the above requirement, plus it seems to be slow as well.
Any suggestions to achieve this?
You can implement your own custom comparer
public class MyEqualityComparer: IEqualityComparer<ClassA> {
public bool Equals(ClassA x, ClassA y) {
if (Object.ReferenceEquals(x, y))
return true;
else if ((null == x) || (null == y))
return false;
// Your custom comparison here (...has to exclude few properties from ClassA)
...
}
public int GetHashCode(ClassA obj) {
if (null == obj)
return 0;
// Your custom hash code based on the included properties
...
}
}
and use HashSet<ClassA> then (we want to exclude oldlist from newlist):
HashSet<ClassA> toExclude = new HashSet<ClassA>(
oldlist,
new MyEqualityComparer());
newList.RemoveAll(item => toExclude.Conytains(item));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With