我有以下课程的两个集合
public class ABC
{
public int studentId {get;set;}
public int schoolId {get;set;}
// Class has other properties too both above two are the keys
}
现在我有两个ABC
的集合 ICollection<ABC> C1 = {Some Data}
ICollection<ABC> C2 = {Some Data}
我想在C1中找到基于键i-e StudentId和SchoolId的C2中不存在的ABC对象
答案 0 :(得分:2)
使用Except
var diff = C1.Except(C2)
请注意,为了跟踪相等性,您可以覆盖Equals方法,或者实现IEqualityComparer并将其传递给Except方法
class ABCEqualityComparer : IEqualityComparer<ABC>
{
public bool Equals(ABC b1, ABC b2)
{
return (b1.studentId == b2.studentId) && (b1.schoolId == b2.schoolId)
}
public int GetHashCode(ABC b)
{
return 7*b.studentId.GetHashCode() + b.schoolId.GetHashCode();
}
}
比你可以使用
var diff = C1.Except(C2, new ABCEqualityComparer())