Get Unique Items from List in C#

Here are some examples of using LINQ to obtain unique/distinct values from a list in C#:

Example 1: Obtaining a Unique List of Integers from a List using LINQ

List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 5, 4, 6, 7, 8, 7, 9 };

var uniqueNumbers = numbers.Distinct();

foreach (var number in uniqueNumbers) {
    Console.WriteLine(number);
}

In this example, a List<int> is initialized with several integer values, some of which are duplicated. The Distinct method is used to obtain a unique list of integers, and then the resulting values are printed to the console using a foreach loop.

Example 2: Obtaining a Unique List of Strings from a List using LINQ

List<string> names = new List<string> { "Alice", "Bob", "Charlie", "Bob", "Dave", "Eve", "Eve" };

var uniqueNames = names.Distinct();

foreach (var name in uniqueNames) {
    Console.WriteLine(name);
}

In this example, a List<string> is initialized with several string values, some of which are duplicated. The Distinct method is used to obtain a unique list of strings, and then the resulting values are printed to the console using a foreach loop.

Example 3: Obtaining a Distinct List from a List of Objects on One or More Properties using LINQ

class Person {
    public string Name { get; set; }
    public int Age { get; set; }
}

List<Person> people = new List<Person> {
    new Person { Name = "Alice", Age = 30 },
    new Person { Name = "Bob", Age = 20 },
    new Person { Name = "Charlie", Age = 25 },
    new Person { Name = "Bob", Age = 30 },
    new Person { Name = "Eve", Age = 40 },
    new Person { Name = "Charlie", Age = 25 }
};

var distinctPeople = people.GroupBy(p => new { p.Name, p.Age }).Select(g => g.First());

foreach (var person in distinctPeople) {
    Console.WriteLine($"{person.Name}, {person.Age}");
}

In this example, a List<Person> is initialized with several Person objects, some of which have the same Name and Age properties. The GroupBy method is used to group the objects by these properties, and then the Select method is used to select the first object in each group, resulting in a distinct list of Person objects. The resulting values are then printed to the console using a foreach loop.

  1. C# get unique items from List:

    List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
    List<int> uniqueNumbers = numbers.Distinct().ToList();
    
  2. Get unique values from List using LINQ in C#:

    List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" };
    List<string> uniqueColors = colors.Distinct().ToList();
    
  3. Remove duplicates from List in C#:

    List<double> prices = new List<double> { 10.5, 20.0, 10.5, 15.75 };
    List<double> uniquePrices = prices.Distinct().ToList();
    
  4. LINQ to get unique items from List in C#:

    List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
    List<int> uniqueNumbers = numbers.GroupBy(n => n).Select(group => group.Key).ToList();
    
  5. C# List distinct values with HashSet:

    List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" };
    HashSet<string> uniqueColors = new HashSet<string>(colors);
    
  6. Retrieve unique elements from List with GroupBy in C#:

    List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
    List<int> uniqueNumbers = numbers.GroupBy(n => n).Select(group => group.Key).ToList();
    
  7. C# List distinct elements by property:

    List<Person> persons = new List<Person> { /* List of Person objects */ };
    List<Person> uniquePersons = persons.DistinctBy(p => p.Id).ToList();
    
  8. Find unique items in List using HashSet in C#:

    List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
    HashSet<int> uniqueNumbersSet = new HashSet<int>(numbers);
    List<int> uniqueNumbers = uniqueNumbersSet.ToList();
    
  9. C# List remove duplicates with LINQ:

    List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" };
    List<string> uniqueColors = colors.Distinct().ToList();
    
  10. Get unique items from List with Dictionary in C#:

    List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
    Dictionary<int, int> uniqueNumbersDict = numbers.ToDictionary(n => n, n => n);
    List<int> uniqueNumbers = uniqueNumbersDict.Keys.ToList();
    
  11. C# List distinct elements using ToHashSet:

    List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" };
    HashSet<string> uniqueColors = colors.ToHashSet();
    
  12. Retrieve unique elements from List with HashSet and Except in C#:

    List<int> numbers = new List<int> { 1, 2, 2, 3, 4, 4, 5 };
    HashSet<int> uniqueNumbersSet = new HashSet<int>(numbers);
    List<int> uniqueNumbers = numbers.Except(uniqueNumbersSet).ToList();
    
  13. C# LINQ distinct elements with anonymous type:

    List<Person> persons = new List<Person> { /* List of Person objects */ };
    var uniquePersons = persons.Select(p => new { p.Id, p.Name }).Distinct().ToList();
    
  14. Find and remove duplicates from List in C#:

    List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" };
    List<string> uniqueColors = colors.GroupBy(c => c).Where(group => group.Count() == 1).Select(group => group.Key).ToList();
    
  15. C# List distinct elements ignoring case:

    List<string> colors = new List<string> { "Red", "green", "Blue", "red", "Green" };
    List<string> uniqueColors = colors.Select(c => c.ToLowerInvariant()).Distinct().ToList();
    
  16. Retrieve unique elements from List using custom equality comparer in C#:

    List<Person> persons = new List<Person> { /* List of Person objects */ };
    List<Person> uniquePersons = persons.Distinct(new PersonEqualityComparer()).ToList();
    
  17. C# LINQ distinct by property:

    List<Person> persons = new List<Person> { /* List of Person objects */ };
    List<Person> uniquePersons = persons.DistinctBy(p => p.Id).ToList();
    
  18. Find and keep only unique items in List in C#:

    List<string> colors = new List<string> { "Red", "Green", "Blue", "Red", "Green" };
    List<string> uniqueColors = colors.GroupBy(c => c).Where(group => group.Count() == 1).Select(group => group.Key).ToList();