2015-10-28 2 views
0

я следующий объект, Role, определяется как:Queryable.Intersect не работает на объекте

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); 
    } 
} 

Прошу совета.

+0

Это результаты.Count(), которые должны дать вам 2? – b729sefc

+0

@ b729sefc, да, это то, чего я ожидаю, однако я получаю 0 –

ответ

1

Вам необходимо внедрить GetHashCode в свой класс Role.

Множество элементов равенства (особенно для хэш-таблиц/наборов, которые Intersect использует за кулисами) ожидает, что оба Equals и GetHashCode будут реализованы вместе последовательно.

Смежные вопросы