0

I have to check equality of two lists including their elements and also their sequence. I am using following but it is not working

    public class SampleClass
    {
        public string Id { get; set; }
        public string Name { get; set; }
    }

    [TestMethod]
    public void EqualityTest()
    {
        var list1 = new List<SampleClass>
        {
            new SampleClass {Id  = "11", Name = "abc"},
            new SampleClass {Id  = "22", Name = "xyz"}
        };
        var list2 = new List<SampleClass>
        {
            new SampleClass {Id  = "11", Name = "abc"},
            new SampleClass {Id  = "22", Name = "xyz"}
        };
        Assert.IsTrue(Enumerable.SequenceEqual(list1, list2));
    }

please suggest any solution.

Next Olive
  • 39
  • 2
  • 6
  • 3
    [CollectionAssert.AreEqual](https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.testtools.unittesting.collectionassert.areequal?view=mstest-net-1.2.0) – bolov Mar 06 '20 at 09:14
  • You can implement [IEquatable](https://learn.microsoft.com/dotnet/api/system.iequatable-1?view=netframework-4.8) – Cid Mar 06 '20 at 09:16
  • 2
    By default the comparison will be done via `object.ReferenceEqual()`, since the `SampleClass` type doesn't implement `IEquatable`. You can fix this by either making `SampleClass` implement `IEquatable` or by using the overload of `SequenceEqual` that accepts a suitable `IEqualityComparer`. – Matthew Watson Mar 06 '20 at 09:16
  • Sorry but it does not work and shows error "CollectionAssert.AreEqual failed. (Element at index 0 do not match.)" – Next Olive Mar 06 '20 at 09:16
  • https://stackoverflow.com/a/38434457/2946329 – Salah Akbari Mar 06 '20 at 09:18
  • Does this answer your question? [Check if two lists are equal](https://stackoverflow.com/questions/22173762/check-if-two-lists-are-equal) – Pavel Anikhouski Mar 06 '20 at 09:26
  • @PavelAnikhouski Seems like OP already uses `SequenceEquals`. The point here is, that the class doesa not implement `IEquatable`and thus `SequenceEquals` falls back to checking for reference-equality. – MakePeaceGreatAgain Mar 06 '20 at 09:30
  • @HimBromBeere Then this [thread](https://stackoverflow.com/questions/50098/comparing-two-collections-for-equality-irrespective-of-the-order-of-items-in-the) will be helpful, it explains `CollectionAssert.AreEqual` method, which is already mentioned above – Pavel Anikhouski Mar 06 '20 at 09:35
  • The problem is `Enumerable.SequenceEqual(list1, list2)` is false, but why ? – Next Olive Mar 06 '20 at 09:42
  • 1
    because it just compares **references** by default. You have to provide some means to compare two instances of your class, either by providing an `IEqualityComparer` or making your class implement `IEquatable`. Only because two instances of `SampleClass` share the same properties, does not mean they are equal. – MakePeaceGreatAgain Mar 06 '20 at 09:48
  • As far as [SequenceEqual](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?redirectedfrom=MSDN&view=netframework-4.8#System_Linq_Enumerable_SequenceEqual__1_System_Collections_Generic_IEnumerable___0__System_Collections_Generic_IEnumerable___0__) is used to determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type. – Next Olive Mar 06 '20 at 10:25

2 Answers2

3

As you've not overridden Equals or implemented IEquatable<SampleClass> you're just getting the default comparison, which checks if the references are equal.

You want something like this:

public class SampleClass : IEquatable<SampleClass>
{
    public string Id { get; set; }
    public string Name { get; set; }

    public bool Equals(SampleClass obj)
    {
      if(obj == null) return false;

      return obj.Id == this.Id && obj.Name == this.Name
    }

    public override bool Equals(object obj) => Equals(obj as SampleClass);
}
Sean
  • 60,939
  • 11
  • 97
  • 136
0

I would recommend using deep comparsion library, like Compare-Net-Objects

Simply import NuGet package

Install-Package CompareNETObjects

And call the helper method

list1.ShouldCompare(list2);
Euphoric
  • 12,645
  • 1
  • 30
  • 44