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
You need to override GetHashCode method in your Employee. You haven't done this. One example of a good hashing method is given below: (generated By ReSharper)
public override int GetHashCode()
{
return ((this.fName != null ? this.fName.GetHashCode() : 0) * 397) ^ (this.lName != null ? this.lName.GetHashCode() : 0);
}
now after Distinct is called, foreach loop prints:
abc def
lmn def
In your case you are calling object's class GetHashCode, which knows nothing about internal fields.
One simple note, MoreLINQ contains DistinctBy extension method, which allows you to do:
IEnumerable coll =
Employeecollection.DistinctBy(employee => new {employee.fName, employee.lName});
Anonymous objects have correct implementation for both GetHashCode and Equals methods.