联盟ING两个自定义类返回重复 [英] Union-ing two custom classes returns duplicates

查看:294
本文介绍了联盟ING两个自定义类返回重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个自定义类, ChangeRequest ChangeRequests ,其中 ChangeRequests 可以包含多个 ChangeRequest 实例。

I have two custom classes, ChangeRequest and ChangeRequests, where a ChangeRequests can contain many ChangeRequest instances.

public class ChangeRequests : IXmlSerializable, ICloneable, IEnumerable<ChangeRequest>,
    IEquatable<ChangeRequests> { ... }

public class ChangeRequest : ICloneable, IXmlSerializable, IEquatable<ChangeRequest>
    { ... }



我试图做的两个工会 ChangeRequests 实例。然而,重复似乎没有被删除。我的MSTest的单元测试如下:

I am trying to do a union of two ChangeRequests instances. However, duplicates do not seem to be removed. My MSTest unit test is as follows:

var cr1 = new ChangeRequest { CRID = "12" };
var crs1 = new ChangeRequests { cr1 };
var crs2 = new ChangeRequests
               {
                   cr1.Clone(),
                   new ChangeRequest { CRID = "34" }
               };
Assert.AreEqual(crs1[0], crs2[0], "First CR in both ChangeRequests should be equal");
var unionedCRs = new ChangeRequests(crs1.Union<ChangeRequest>(crs2));
ChangeRequests expected = crs2.Clone();
Assert.AreEqual(expected, unionedCRs, "Duplicates should be removed from a Union");



该测试的最后一行失败, unionedCRs 包含 CR1 。当我试图调试,并通过每行一步,我,以及在<$的第一行曾在 ChangeRequest.Equals(对象)第一行设置断点C $ C> ChangeRequest.Equals(ChangeRequest),但也被击中。为什么工会包含重复 ChangeRequest 实例

The test fails in the last line, and unionedCRs contains two copies of cr1. When I tried to debug and step through each line, I had a breakpoint in ChangeRequest.Equals(object) on the first line, as well as in the first line of ChangeRequest.Equals(ChangeRequest), but neither were hit. Why does the union contain duplicate ChangeRequest instances?

编辑:的要求,在这里在 ChangeRequests.Equals(ChangeRequests)

public bool Equals(ChangeRequests other)
{
    if (ReferenceEquals(this, other))
    {
        return true;
    }

    return null != other && this.SequenceEqual<ChangeRequest>(other);
}

和这里的 ChangeRequests.Equals(对象)

public override bool Equals(object obj)
{
    return Equals(obj as ChangeRequests);
}



编辑:我推翻的GetHashCode 两个 ChangeRequest ChangeRequests ,但仍然在我的测试,如果我这样做的IEnumerable< ChangeRequest> unionedCRsIEnum = crs1.Union< ChangeRequest>(crs2); unionedCRsIEnum 带的两个副本结束的 ChangeRequest CRID 12

I overrode GetHashCode on both ChangeRequest and ChangeRequests but still in my test, if I do IEnumerable<ChangeRequest> unionedCRsIEnum = crs1.Union<ChangeRequest>(crs2);, unionedCRsIEnum ends up with two copies of the ChangeRequest with CRID 12.

编辑:的东西必须是与我的等于的GetHashCode 实现的地方,因为 Assert.AreEqual(预期,unionedCRs。不同的(),明显的应该删除重复项); 失败,并且的字符串表示有望 unionedCRs.Distinct ()显示, unionedCRs.Distinct()绝对有CR 12的两个副本。

something has to be up with my Equals or GetHashCode implementations somewhere, since Assert.AreEqual(expected, unionedCRs.Distinct(), "Distinct should remove duplicates"); fails, and the string representations of expected and unionedCRs.Distinct() show that unionedCRs.Distinct() definitely has two copies of CR 12.

推荐答案

请确保您的的GetHashCode 的实施是与你的等于 - 中 Enumerable.Union 方法确实出现了同时使用。

Make sure your GetHashCode implementation is consistent with your Equals - the Enumerable.Union method does appear to use both.

您应该从编译器得到一个警告,如果你已经实施之一,但不是其他;它仍然是由你来确保这两种方法相互一致。下面是规则的方便总结:的 http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overriden-in-c

You should get a warning from the compiler if you've implemented one but not the other; it's still up to you to make sure that both methods agree with each other. Here's a convenient summary of the rules: http://stackoverflow.com/questions/371328/why-is-it-important-to-override-gethashcode-when-equals-method-is-overriden-in-c

这篇关于联盟ING两个自定义类返回重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆