Listnumbers = new List { 1, 2, 2, 3, 3, 3 }; List distinctNumbers = numbers.Distinct().ToList(); // distinctNumbers now contains { 1, 2, 3 }
public class Person { public string Name { get; set; } public int Age { get; set; } } ListThe System.Collections.Generic.List and System.Collections.Generic namespaces are part of the .NET Framework Class Library.people = new List { new Person { Name = "John", Age = 30 }, new Person { Name = "Mary", Age = 25 }, new Person { Name = "John", Age = 35 }, }; List distinctPeople = people.Distinct(new PersonNameComparer()).ToList(); public class PersonNameComparer : IEqualityComparer { public bool Equals(Person x, Person y) { if (ReferenceEquals(x, y)) return true; if (x == null || y == null) return false; return x.Name == y.Name; } public int GetHashCode(Person obj) { return obj.Name.GetHashCode(); } } // distinctPeople now contains { John (30), Mary (25) }