Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove duplicates from collection using IEqualityComparer, LinQ Distinct

Tags:

c#

list

linq

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 void Main(string[] args)     {         List<Employe> Employeecollection = new List<Employe>();          Employeecollection.Add(new Employe("abc","def"));         Employeecollection.Add(new Employe("lmn","def"));         Employeecollection.Add(new Employe("abc", "def"));          IEnumerable<Employe> coll = Employeecollection.Distinct(new Employe());          foreach (Employe item in coll)         {             Console.WriteLine(item.fName + "   " + item.lName );         }      } 

The Below is the Employee class implementation , here i implemented IEqualityComparer

class Employe : IEqualityComparer<Employe> {     public string fName { get; set; }     public string lName { get; set; }      public Employe()     {      }      public Employe(string firstName, string LastName)     {         this.fName = firstName;         this.lName = LastName;     }      #region IEqualityComparer<pcf> Members      public bool Equals(Employe x, Employe y)     {         if (x.fName == y.fName && x.lName == y.lName)         {             return true;         }          return false;     }      public int GetHashCode(Employe obj)     {         return obj.GetHashCode();     }      #endregion } 
like image 353
Gun Avatar asked Jun 07 '13 11:06

Gun


People also ask

How do I remove duplicates in LINQ query?

We can remove all duplicates like this by using GroupBy and Select methods provided by LINQ . First, we group our list so that we will create groups for each name in the List. Then we use Select to only select the first objects in each group. This will result in the removal of the duplicates.

Does Linq distinct use equals?

c# - Linq distinct doesn't call Equals method - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

Does Linq Union remove duplicates?

Linq, acts upon 2 collections. It returns a new collection that contains the elements that are found. Union removes duplicates. So this method can be thought of as two actions: it combines the two collections and then uses Distinct() on them, removing duplicate elements.

What does distinct do in Linq?

C# Linq Distinct() method removes the duplicate elements from a sequence (list) and returns the distinct elements from a single data source. It comes under the Set operators' category in LINQ query operators, and the method works the same way as the DISTINCT directive in Structured Query Language (SQL).


2 Answers

Forget IEqualityComparer and just use Linq directly:

EmployeeCollection.GroupBy(x => new{x.fName, x.lName}).Select(g => g.First()); 
like image 173
spender Avatar answered Oct 11 '22 19:10

spender


Here is a good tutorial

    public int GetHashCode(Employe obj)     {         return obj.fname.GetHashCode() ^ obj.lname.GetHashCode();     } 
like image 23
Nikola Mitev Avatar answered Oct 11 '22 20:10

Nikola Mitev