【发布时间】:2014-10-20 15:34:57
【问题描述】:
我正在尝试设置一个使用字符串列表作为比较属性的 IEqualityComparer。
在下面的 2 行代码中使用 except 和 Intersect 时,所有记录都被视为“新”,没有一个被识别为“旧”。
List<ExclusionRecordLite> newRecords = currentRecords.Except(historicalRecords, new ExclusionRecordLiteComparer()).ToList();
List<ExclusionRecordLite> oldRecords = currentRecords.Intersect(historicalRecords, new ExclusionRecordLiteComparer()).ToList();
这是我的 IEqualityComparer 类(单词是一个列表)
public class RecordComparer : IEqualityComparer<Record>
{
public bool Equals(Record x, Record y)
{
if (object.ReferenceEquals(x, y))
return true;
if (x == null || y == null)
return false;
return x.Words.SequenceEqual(y.Words);
}
public int GetHashCode(Record obj)
{
return new { obj.Words }.GetHashCode();
}
}
【问题讨论】:
标签: c# iequalitycomparer