Implement IEqualityComparer

后端 未结 3 1040
Happy的楠姐
Happy的楠姐 2021-02-14 09:38

I would like to get distinct objects from a list. I tried to implement IEqualityComparer but wasn\'t successful. Please review my code and give me an explanation fo

相关标签:
3条回答
  • 2021-02-14 10:24

    You don't need to implement IEqualityComparer:

    public class Message
    {
        protected bool Equals(Message other)
        {
            return string.Equals(x, other.x) && string.Equals(y, other.y) && string.Equals(z, other.z) && string.Equals(w, other.w);
        }
    
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((Message) obj);
        }
    
        public override int GetHashCode()
        {
            unchecked //Ignores overflows that can (should) occur
            {
                var hashCode = x;
                hashCode = (hashCode*397) ^ (y != null ? y.GetHashCode() : 0);
                hashCode = (hashCode*397) ^ (z != null ? z.GetHashCode() : 0);
                hashCode = (hashCode*397) ^ (w != null ? w.GetHashCode() : 0);
                return hashCode;
            }
        }
    
        public int x { get; set; }
        public string y { get; set; }
        public string z { get; set; }
        public string w { get; set; }
    }
    
    0 讨论(0)
  • 2021-02-14 10:29

    IEqualityComparer is an interface which is used to find whether an object is equal or not. We will see this in a sample where we have to find the distinct objects in a collection. This interface will implement the method Equals(T obj1,T obj2).

    abstract public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { set; get; }
    }
    
    public enum SortType
    {
        ByID,
        BySalary
    }
    
    public class EmployeeDistinctEquality : IEqualityComparer<Employee>
    {
        public EmployeeDistinctEquality()
        {
    
        }
    
        public bool Equals(Employee x, Employee y)
        {
            if (x == null && y == null)
                return true;
            else if (x == null || y == null)
                return false;
            else if (x.Id == y.Id)
                return true;
            else
                return false;
        }
    
        public int GetHashCode(Employee obj)
        {
            return obj.Id.GetHashCode();
        }
    }
    

    Refer to this link for more detailed information:

    http://dotnetvisio.blogspot.in/2015/12/usage-of-icomparer-icomparable-and.html

    0 讨论(0)
  • 2021-02-14 10:36

    Try this:

    var distinct = collection.Distinct(new MessageComparer());
    

    Then use distinct for anything after that.

    It looks like you're forgetting the immutable nature of IEnumerable<>. None of the LINQ methods actually change the original variable. Rather, they return IEnuerable<T>s which contain the result of the expression. For example, let's consider a simple List<string> original with the contents { "a", "a", "b", "c" }.

    Now, let's call original.Add("d");. That method has no return value (it's void). But if we then print out the contents of original, we will see { "a", "a", "b", "c", "d" }.

    On the other hand, let's now call original.Skip(1). This method does have a return value, one of type IEnumerable<string>. It is a LINQ expression, and performs no side-effecting actions on the original collection. Thus, if we call that and look at original, we will see { "a", "a", "b", "c", "d" }. However, the result from the method will be { "a", "b", "c", "d" }. As you can see, the result skips one element.

    This is because LINQ methods accept IEnumerable<T> as a parameter. Consequently, they have no concept of the implementation of the original list. You could be passing, via extension method, a ReadOnlyCollection and they would still be able to evaluate through it. They cannot, then, alter the original collection, because the original collection could be written in any number of ways.

    All that, but in table form. Each lines starts with the original { "a", "a", "b", "c" }:

    Context     Example function    Immutable?    Returned Value     Collection after calling
    Collection  Add("d")            No            (void)             { "a", "a", "b", "c", "d" }:
    LINQ        Skip(1)             Yes           { "a", "b", "c" }  { "a", "a", "b", "c" }:
    
    0 讨论(0)
提交回复
热议问题