How to remove duplicates from collection using IEqualityComparer, LinQ Distinct

前端 未结 6 1845
梦如初夏
梦如初夏 2020-12-05 04:36

I am unable to remove the duplicates from collection , i have implemented IEqualityComparer for the class Employee still i am not getting the output

static v         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 04:51

    Also it looks like your comparing by reference instead of content, hence the compare function doesn't work.

    change it to use .Equals() instead of == and it should work. example below:

    #region IEqualityComparer Members
    
    public bool Equals(Employe x, Employe y)
    {
        if (x.fName.Equals(y.fName) && x.lName.Equals(y.lName))
        {
            return true;
        }
    
        return false;
    }
    
    public int GetHashCode(Employe obj)
    {
        return obj.GetHashCode();
    }
    
    #endregion
    

提交回复
热议问题