IEqualityComparer 使用两个属性失败
IEqualityComparer fails using two properties
当我在 Distinct() 中使用这个比较器时,它总是 returns 错误。看不出原因。
public class IdEqualityComparer : IEqualityComparer<Relationship>
{
public bool Equals(Relationship x, Relationship y)
{
if (x == null && y == null)
return true;
else if (x == null || y == null)
return false;
else if (x.ID == y.ID && x.RelatedID == y.RelatedID)
return true;
else
return false;
}
public int GetHashCode(Relationship obj)
{
unchecked
{
int hash = (obj.ID ?? "").GetHashCode() ^ (obj.RelatedID ?? "").GetHashCode();
return hash;
}
}
}
哈希对我来说似乎是正确的,但 ID 和 RelatedID 的比较永远不会 returns 正确。
它失败了,因为我可以在之后检查结果并且使用这两个属性的输出并不明显。
这里似乎工作正常;
static void Main()
{
var objs = new[]
{
new Relationship { ID = "a", RelatedID = "b" }, // <----\
new Relationship { ID = "a", RelatedID = "c" }, // |
new Relationship { ID = "a", RelatedID = "b" }, // dup--/
new Relationship { ID = "d", RelatedID = "b" }, // <------\
new Relationship { ID = "d", RelatedID = "c" }, // |
new Relationship { ID = "d", RelatedID = "b" }, // dup ---/
new Relationship { ID = "b", RelatedID = "c" }, //
};
var count = objs.Distinct(new IdEqualityComparer()).Count();
System.Console.WriteLine(count);
}
给出 5
,而不是 7
(如果它总是返回 false
,我们会期望)。测试:
public class Relationship
{
public string ID { get; set; }
public string RelatedID { get; set; }
}
为了更清楚地说明这一点:
var a = new Relationship { Id = "x", RelatedID = "foo" };
var b = new Relationship { Id = "y", RelatedID = "foo" };
var c = new Relationship { Id = "x", RelatedID = "foo" };
我们现在可以适当地证明比较器 returns true
和 false
:
var comparer = new IdEqualityComparer();
Console.WriteLine(comparer.Equals(a, b)); // False
Console.WriteLine(comparer.Equals(a, c)); // True
我们还可以证明 hash-code 正常工作:
Console.WriteLine(comparer.GetHashCode(a));
Console.WriteLine(comparer.GetHashCode(b));
Console.WriteLine(comparer.GetHashCode(c));
请注意,数字会发生变化,但是 对我来说 运行 这给出了:
-789327704
1132350395
-789327704
数字并不重要 - 重要的是第一个和最后一个相等,并且(理想情况下)与中间那个不同。
所以:比较器工作正常,问题的前提不正确。您需要确定代码中的不同之处,并加以修复。
向大家致歉!它工作正常。
我正在比较与 Marc 的答案类似定义的对象。但是,我这样做了:
var relators = relationships.Distinct(new RelationshipEqualityComparer());
然后将关系而不是房地产经纪人发送到审查项目的下一个方法。有时需要另一双眼睛!
谢谢!
当我在 Distinct() 中使用这个比较器时,它总是 returns 错误。看不出原因。
public class IdEqualityComparer : IEqualityComparer<Relationship>
{
public bool Equals(Relationship x, Relationship y)
{
if (x == null && y == null)
return true;
else if (x == null || y == null)
return false;
else if (x.ID == y.ID && x.RelatedID == y.RelatedID)
return true;
else
return false;
}
public int GetHashCode(Relationship obj)
{
unchecked
{
int hash = (obj.ID ?? "").GetHashCode() ^ (obj.RelatedID ?? "").GetHashCode();
return hash;
}
}
}
哈希对我来说似乎是正确的,但 ID 和 RelatedID 的比较永远不会 returns 正确。
它失败了,因为我可以在之后检查结果并且使用这两个属性的输出并不明显。
这里似乎工作正常;
static void Main()
{
var objs = new[]
{
new Relationship { ID = "a", RelatedID = "b" }, // <----\
new Relationship { ID = "a", RelatedID = "c" }, // |
new Relationship { ID = "a", RelatedID = "b" }, // dup--/
new Relationship { ID = "d", RelatedID = "b" }, // <------\
new Relationship { ID = "d", RelatedID = "c" }, // |
new Relationship { ID = "d", RelatedID = "b" }, // dup ---/
new Relationship { ID = "b", RelatedID = "c" }, //
};
var count = objs.Distinct(new IdEqualityComparer()).Count();
System.Console.WriteLine(count);
}
给出 5
,而不是 7
(如果它总是返回 false
,我们会期望)。测试:
public class Relationship
{
public string ID { get; set; }
public string RelatedID { get; set; }
}
为了更清楚地说明这一点:
var a = new Relationship { Id = "x", RelatedID = "foo" };
var b = new Relationship { Id = "y", RelatedID = "foo" };
var c = new Relationship { Id = "x", RelatedID = "foo" };
我们现在可以适当地证明比较器 returns true
和 false
:
var comparer = new IdEqualityComparer();
Console.WriteLine(comparer.Equals(a, b)); // False
Console.WriteLine(comparer.Equals(a, c)); // True
我们还可以证明 hash-code 正常工作:
Console.WriteLine(comparer.GetHashCode(a));
Console.WriteLine(comparer.GetHashCode(b));
Console.WriteLine(comparer.GetHashCode(c));
请注意,数字会发生变化,但是 对我来说 运行 这给出了:
-789327704
1132350395
-789327704
数字并不重要 - 重要的是第一个和最后一个相等,并且(理想情况下)与中间那个不同。
所以:比较器工作正常,问题的前提不正确。您需要确定代码中的不同之处,并加以修复。
向大家致歉!它工作正常。
我正在比较与 Marc 的答案类似定义的对象。但是,我这样做了:
var relators = relationships.Distinct(new RelationshipEqualityComparer());
然后将关系而不是房地产经纪人发送到审查项目的下一个方法。有时需要另一双眼睛!
谢谢!