Linq Distinct 不按照论坛和Microsoft示例运行

本文关键字:Microsoft 运行 论坛 Distinct Linq | 更新日期: 2023-09-27 18:21:16

using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace UnitTest.Model
{
[TestFixture]
public class SampleEquatableObjectTest
{
    [Test]
    public void TwoIdenticalUsersComparedEqualTrue()
    {
        var user1 = new SampleObject { Id = 1, Name = "Test User" };
        var user2 = new SampleObject { Id = 1, Name = "Test User" };
        Assert.IsTrue(user1.Equals(user2));
    }
    [Test]
    public void TwoDifferentUsersComparedEqualFalse()
    {
        var user1 = new SampleObject { Id = 1, Name = "Test User 1" };
        var user2 = new SampleObject { Id = 2, Name = "Test User 2" };
        Assert.IsFalse(user1.Equals(user2));
    }
    [Test]
    public void CollectionOfUsersReturnsDistinctList()
    {
        var userList = new List<SampleObject>
                           {
                               new SampleObject {Id = 1, Name = "Test User"},
                               new SampleObject {Id = 1, Name = "Test User 1"},
                               new SampleObject {Id = 2, Name = "Test User 2"}
                           };
        Assert.AreEqual(userList.Count, 3);
        var result = userList.Distinct();
        Assert.AreEqual(result.Count(), 2);
        var multipleTest = (from r in result group r by new { r.Id } into multGroup where multGroup.Count() > 1 select multGroup.Key).Any();
        Assert.IsFalse(multipleTest);
    }
    public class SampleObject : IEquatable<SampleObject>
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool Equals(SampleObject other)
        {
            if (ReferenceEquals(this, other))
                return true;
            if (ReferenceEquals(other, null) || ReferenceEquals(this, null))
                return false;
            return Id.Equals(other.Id);
        }
    }
}

}

此测试用例中的非重复方法不会返回非重复列表。计数断言将失败。我查看了其他类似的问题和Microsoft示例,但它们看起来与我在测试中的代码完全相同。任何输入?

Linq Distinct 不按照论坛和Microsoft示例运行

您还需要从 Object 类重写 GetHashCode(( 和 Equals 方法。有关更多信息,请参阅此相应的 FXCOP 违规。

然后,您的测试将按预期工作。

public class SampleObject : IEquatable<SampleObject>
{
    public int Id { get; set; }
    public string Name { get; set; }
    public bool Equals(SampleObject other)
    {
        if (ReferenceEquals(this, other))
            return true;
        if (ReferenceEquals(other, null) || ReferenceEquals(this, null))
            return false;
        return Id.Equals(other.Id);
    }
    public override int GetHashCode() 
    {
        return Id;
    }
    public override bool Equals(object obj) 
    {
        return Equals(obj as SampleObject);
    }
}

您正在实现一个接口,而无需根据接口的规范定义两个成员。 您的代码需要如下所示:

    public bool Equals(SampleObject x, SampleObject y)
    {
        if (ReferenceEquals(x, y))
            return true;
        if (ReferenceEquals(x, null) || ReferenceEquals(y, null))
            return false;
        return x.Id.Equals(y.Id);
    }
    public int GetHashCode(SampleObject obj)
    {
               public int GetHashCode(SampleObject obj)
    {
        if (Object.ReferenceEquals(obj, null)) return 0;
        int hashId = obj.Id == null ? 0 : obj.Id.GetHashCode();
        int hashName = obj.Name == null ? 0 : obj.Name.GetHashCode();
        return hashId ^ hashName; // or what ever you want you hash to be, hashID would work just as well.
    }
    }