Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing duplicates in a list with linq

Tags:

c#

linq

Suppose you have a list of MyObject like this:

public class MyObject
{
  public int ObjectID {get;set;}
  public string Prop1 {get;set;}
}

How do you remove duplicates from a list where there could be multiple instance of objects with the same ObjectID.

Thanks.

like image 309
frenchie Avatar asked May 11 '11 19:05

frenchie


People also ask

How to remove duplicates from the list in c#?

Use the Distinct() method to remove duplicates from a list in C#.

How do I find duplicate records in Linq?

If you want to know how many times the elements are repeated, you can use: var query = lst. GroupBy(x => x) .

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.

Can list have duplicates in C#?

We can use the Enumerable. GroupBy() method to group the elements based on their value, then filters out the groups that appear only once, leaving them out with duplicates keys.


3 Answers

You can use GroupBy() and select the first item of each group to achieve what you want - assuming you want to pick one item for each distinct ObjectId property:

var distinctList = myList.GroupBy(x => x.ObjectID)                          .Select(g => g.First())                          .ToList(); 

Alternatively there is also DistinctBy() in the MoreLinq project that would allow for a more concise syntax (but would add a dependency to your project):

var distinctList = myList.DistinctBy( x => x.ObjectID).ToList(); 
like image 112
BrokenGlass Avatar answered Sep 18 '22 12:09

BrokenGlass


You can do this using the Distinct() method. But since that method uses the default equality comparer, your class needs to implement IEquatable<MyObject> like this:

public class MyObject : IEquatable<MyObject> {     public int ObjectID {get;set;}     public string Prop1 {get;set;}      public bool Equals(MyObject other)     {         if (other == null) return false;         else return this.ObjectID.Equals(other.ObjectID);      }      public override int GetHashCode()     {         return this.ObjectID.GetHashCode();     } } 

Now you can use the Distinct() method:

List<MyObject> myList = new List<MyObject>(); myList.Add(new MyObject { ObjectID = 1, Prop1 = "Something" }); myList.Add(new MyObject { ObjectID = 2, Prop1 = "Another thing" }); myList.Add(new MyObject { ObjectID = 3, Prop1 = "Yet another thing" }); myList.Add(new MyObject { ObjectID = 1, Prop1 = "Something" });  var duplicatesRemoved = myList.Distinct().ToList(); 
like image 40
Kristof Claes Avatar answered Sep 16 '22 12:09

Kristof Claes


You could create a custom object comparer by implementing the IEqualityComparer interface:

public class MyObject
{
    public int Number { get; set; }
}

public class MyObjectComparer : IEqualityComparer<MyObject>
{
    public bool Equals(MyObject x, MyObject y)
    {
        return x.Id == y.Id;
    }

    public int GetHashCode(MyObject obj)
    {
        return obj.Id.GetHashCode();
    }
}

Then simply:

myList.Distinct(new MyObjectComparer()) 
like image 41
DigitalNomad Avatar answered Sep 17 '22 12:09

DigitalNomad