Kee*_*ser 3 c# linq list hashset
List<ClassA> newlist;
List<ClassA> oldlist;
Run Code Online (Sandbox Code Playgroud)
ClassA 有20种不同的属性,我想
ClassA因为值不是相关的Linq Except或Intersect似乎不支持上述要求,而且似乎也很慢.有任何建议来实现这一目标
您可以实现自己的自定义比较器
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
...
}
}
Run Code Online (Sandbox Code Playgroud)
并使用HashSet<ClassA>,然后(我们要排除oldlist来自newlist):
HashSet<ClassA> toExclude = new HashSet<ClassA>(
oldlist,
new MyEqualityComparer());
newList.RemoveAll(item => toExclude.Conytains(item));
Run Code Online (Sandbox Code Playgroud)