更新时间:2023-02-26 22:01:36
问题出在您的GetHashCode
上.您应该对其进行更改以返回AllianceName
的哈希码.
The problem is with your GetHashCode
. You should alter it to return the hash code of AllianceName
instead.
int IEqualityComparer<Village>.GetHashCode(Village obj)
{
return obj.AllianceName.GetHashCode();
}
问题是,如果Equals
返回true
,则对象应具有相同的哈希码,而对于具有相同AllianceName
的不同Village
对象则不是这样.由于Distinct
是通过内部构建哈希表来工作的,因此最终您将得到相等的对象,这些对象由于不同的哈希码而根本无法匹配.
The thing is, if Equals
returns true
, the objects should have the same hash code which is not the case for different Village
objects with same AllianceName
. Since Distinct
works by building a hash table internally, you'll end up with equal objects that won't be matched at all due to different hash codes.
类似地,要比较两个文件,如果两个文件的哈希值不相同,则根本不需要检查文件本身.它们将不同.否则,您将继续检查它们是否真的相同.这正是Distinct
使用的哈希表的行为.
Similarly, to compare two files, if the hash of two files are not the same, you don't need to check the files themselves at all. They will be different. Otherwise, you'll continue to check to see if they are really the same or not. That's exactly what the hash table that Distinct
uses behaves.