Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# list -Eliminate Duplicate items by comparing specific properties

List<ClassA> newlist;
List<ClassA> oldlist;

ClassA has 20 different properties, I want to

  • compare and remove exact matching items from newlist
  • however the comparison has to exclude few properties from ClassA as the values will not be relavant
  • Record set I am dealing with is huge (300 thousand to 400 thousand). So it has to be efficient

Linq Except or Intersect doesnt seem to support the above requirement, plus it seems to be slow as well. Any suggestions to achieve this?

like image 494
KeenUser Avatar asked Jan 23 '26 14:01

KeenUser


1 Answers

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));
like image 69
Dmitry Bychenko Avatar answered Jan 25 '26 03:01

Dmitry Bychenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!