可查询.Intersect无法处理对象

本文关键字:处理 对象 查询 Intersect | 更新日期: 2023-09-27 18:19:28

我有以下对象Role,定义为:

public class Role
{
    public String RoleName { get; set; }
    public Guid RoleGuid { get; set; }
    public override bool Equals(object obj)
    {
        return (this.RoleName == ((Role)obj).RoleName);
    }
}

我还有一个集合建模为RolesCollection,定义为:

public class RolesCollection : IList<Role>, IEqualityComparer<Role>, IEnumerable<Role>
{
    private readonly IList<Role> _list = new List<Role>();
    public Role this[int index]
    {
        get
        {
            return _list[index];
        }
        set
        {
            _list[index] = value;
        }
    }
    public int Count
    {
        get
        {
            return _list.Count;
        }
    }
    public bool IsReadOnly
    {
        get
        {
            return _list.IsReadOnly;
        }
    }
    public void Add(Role item)
    {
        _list.Add(item);
    }
    public void Clear()
    {
        _list.Clear();
    }
    public bool Contains(Role item)
    {
        return _list.Contains(item);
    }
    public void CopyTo(Role[] array, int arrayIndex)
    {
        _list.CopyTo(array, arrayIndex);
    }
    public bool Equals(Role x, Role y)
    {
        return (x.RoleName == y.RoleName);
    }
    public IEnumerator<Role> GetEnumerator()
    {
        return _list.GetEnumerator();
    }
    public int GetHashCode(Role obj)
    {
        return obj.RoleName.GetHashCode();
    }
    public int IndexOf(Role item)
    {
        Role interalItem = _list.First<Role>(i => i.RoleName == item.RoleName);
        return _list.IndexOf(interalItem);
    }
    public void Insert(int index, Role item)
    {
        _list.Insert(index, item);
    }
    public bool Remove(Role item)
    {
        return _list.Remove(item);
    }
    public void RemoveAt(int index)
    {
        _list.RemoveAt(index);
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

现在是我的单元Tests.EqualsTest(),它正在通过,RolesIntersectTest(),它正在失败。它应该给我2,但它给了我0。

[TestClass()]
public class RoleTests
{
    public RolesCollection Source { get; set; }
    public RolesCollection Destination { get; set; }
    [TestInitialize()]
    public void SetUp() {
        Source = new RolesCollection();
        Source.Add(new Role() { RoleGuid = System.Guid.NewGuid(), RoleName = "Baker" });
        Source.Add(new Role() { RoleGuid = System.Guid.NewGuid(), RoleName = "Tailor" });
        Source.Add(new Role() { RoleGuid = System.Guid.NewGuid(), RoleName = "Chef" });
        Destination = new RolesCollection();
        Destination.Add(new Role() { RoleGuid = System.Guid.NewGuid(), RoleName = "Baker" });
        Destination.Add(new Role() { RoleGuid = System.Guid.NewGuid(), RoleName = "Tailor" });
        Destination.Add(new Role() { RoleGuid = System.Guid.NewGuid(), RoleName = "Teacher" });
    }
    [TestMethod()]
    public void EqualsTest()
    {
        Role role1 = new Role() {
            RoleGuid = System.Guid.NewGuid(),
            RoleName = "Vinay"
        };
        Role role2 = new Role()
        {
            RoleGuid = System.Guid.NewGuid(),
            RoleName = "Vinay"
        };
        Assert.IsTrue(role1.Equals(role2));
    }
    [TestMethod()]
    public void RolesIntersectTest() {
        RolesCollection results = Source.Intersect(Destination) as RolesCollection;
        Assert.IsTrue(results.Count() > 1);
    }
}

请提供建议。

可查询.Intersect无法处理对象

您需要在Role类上实现GetHashCode。

很多相等的东西(尤其是Intersect在幕后使用的哈希表/集)都希望Equals和GetHashCode能够一致地一起实现。