c# remove duplicates from list of objects code example
Example 1: c# find duplicates in list
var query = lst.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(y => y.Key)
.ToList();
Example 2: remove duplicated from object array c#
import System.Linq;
int[] nums = { 1, 2, 3, 4, 3, 55, 23, 2 };
int[] dist = nums.Distinct().ToArray();
class Order{
public int i, j;
Order(int i, int j){
this.i = i;
this.j = j;
}
}
class Comparer : IEqualityComparer<Connection> {
public bool Equals(Order x, Order y) {
return x.i == y.i;
}
public int GetHashCode(Order obj) {
return obj.i.GetHashCode();
}
}
List<Order> list = new List<Order>();
list.Add(1, 2);
list.Add(2, 3);
list.Add(1, 4);
list.Add(4, 5);
list.Add(1, 6);
List<Order> rem_dup = list.Distinct(new Comparer()).ToList();
Example 3: c# list remove item based on property duplicate
fooArray.GroupBy(x => x.Id).Select(x => x.First());