Combining two custom classes returns duplicates

I have two custom classes ChangeRequestand ChangeRequests, where ChangeRequestscan contain many instances ChangeRequest.

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

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

I am trying to do a merge of two instances ChangeRequests. However, duplicates are not deleted. 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");

The test failed on the last line and unionedCRscontains two copies cr1. When I tried to debug and go through each line, I had a breakpoint in ChangeRequest.Equals(object)the first line as well as the first line ChangeRequest.Equals(ChangeRequest), but none of them were deleted. Why does the union contain duplicate instances ChangeRequest?

Edit: on request, here ChangeRequests.Equals(ChangeRequests):

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

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

And here ChangeRequests.Equals(object):

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.

: - Equals GetHashCode, Assert.AreEqual(expected, unionedCRs.Distinct(), "Distinct should remove duplicates"); , expected unionedCRs.Distinct() , unionedCRs.Distinct() CR 12.

+3
3

, GetHashCode Equals - Enumerable.Union, , .

, , ; , . : GetHashCode, Equals ?

+4

, Assert.AreEqual() - , .

, , SequenceEqual(), . . , , IEnumerable<> .

, :

public static class AssertionExt
{
  public static bool AreSequencesEqual<T>( IEnumerable<T> expected, 
                                           IEnumerable<T> sequence )
  {
    Assert.AreEqual(expected.Count(), sequence .Count()); 

    IEnumerator<Token> e1 = expected.GetEnumerator(); 
    IEnumerator<Token> e2 = sequence .GetEnumerator(); 

    while (e1.MoveNext() && e2.MoveNext()) 
    { 
        Assert.AreEqual(e1.Current, e2.Current); 
    }
  }
}

SequenceEqual(), , , , .

+3

. , Assert.AreEqual Equals .

SequenceEqual :

Assert.IsTrue(expected.SequenceEqual(unionedCRs));

, .

You can use the test code that we wrote for MoreLINQ , which was sequence oriented - if the sequences are not equal, this will determine how they differ. (I'm trying to get a link to the source file in question, but my network connection is garbage.)

0
source

Source: https://habr.com/ru/post/1752105/


All Articles