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.
Use the Distinct() method to remove duplicates from a list in C#.
If you want to know how many times the elements are repeated, you can use: var query = lst. GroupBy(x => x) .
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.
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.
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();
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();
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With