Distinct() не работает - C#
Формулировка задачи:
Листинг программы
- public class RouteRel : IEquatable<RouteRel>
- {
- public long Id { get; set; }
- public string Ref { get; set; }
- public string Name { get; set; }
- public string Description { get; set; }
- public int GetHashCode( RouteRel obj )
- {
- return obj.Id.GetHashCode();
- }
- public bool Equals( RouteRel other )
- {
- if ( Object.ReferenceEquals( this, other ) )
- return true;
- if ( Object.ReferenceEquals( other, null ) )
- return false;
- return EqualsHelper( this, other );
- }
- protected static bool EqualsHelper( RouteRel r1, RouteRel r2 )
- {
- return r1.Id == r2.Id || r1.Ref == r2.Ref;
- }
- }
Листинг программы
- List<RouteRel> routes = new List<RouteRel>();
- /* здесь инициализация */
- routes = routes.Distinct().ToList();
Листинг программы
- routes[ 2 ].Equals( routes[ 3 ] );
Решение задачи: «Distinct() не работает»
textual
Листинг программы
- public override bool Equals(object obj)
- {
- if (ReferenceEquals(null, obj))
- {
- return false;
- }
- if (ReferenceEquals(this, obj))
- {
- return true;
- }
- if (obj.GetType() != GetType())
- {
- return false;
- }
- return Equals((RouteRel)obj);
- }
- public bool Equals(RouteRel other)
- {
- return Id == other.Id || string.Equals(Ref, other.Ref);
- }
- public override int GetHashCode()
- {
- unchecked
- {
- return (Id.GetHashCode() * 397) | (Ref != null ? Ref.GetHashCode() : 0);
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д