LINQ Distinct Method in C#

LINQ (Language-Integrated Query) is a powerful feature of C# that enables developers to query data from various data sources. One of the key methods in LINQ is the Distinct method, which is used to retrieve unique elements from a collection. In this lesson, we will discuss the Distinct method in detail and explore how it works in C#.

What is the Distinct Method in C#? The Distinct method is a LINQ extension method that returns a new collection with unique elements from the source collection. It compares the elements in the source collection by their default equality comparer, and returns only the distinct elements in the new collection. The Distinct method is applied to a collection of objects that implement the IEquatable<T> interface or use the default equality comparer.

Syntax: The syntax for using the Distinct method in C# is as follows:
IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source);

Where,

  • source: The input collection to be processed.
  • TSource: The type of the elements in the input collection.

Example: Consider the following example, where we have a collection of integers and we want to retrieve the unique elements from the collection using the Distinct method.

int[] numbers = { 1, 2, 2, 3, 4, 4, 5 };
var uniqueNumbers = numbers.Distinct();

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

Output:

1
2
3
4
5

Explanation: In the above example, we have a collection of integers with duplicate elements. We apply the Distinct method to the collection and store the result in a new variable called uniqueNumbers. The Distinct method compares the elements in the collection and returns only the unique elements. Finally, we iterate through the uniqueNumbers collection using a foreach loop and print each element to the console.

Custom Equality Comparer:

In some cases, the default equality comparer may not be suitable for comparing the elements in the collection. In such cases, we can provide a custom equality comparer to the Distinct method. The custom equality comparer should implement the IEqualityComparer<T> interface and provide a method for comparing the elements.

Example: Consider the following example, where we have a collection of Person objects, and we want to retrieve the unique elements based on the Person’s Name property.

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

public class PersonNameEqualityComparer : IEqualityComparer<Person>
{
    public bool Equals(Person x, Person y)
    {
        if (x == null || y == null)
            return false;

        return x.Name == y.Name;
    }

    public int GetHashCode(Person obj)
    {
        return obj.Name.GetHashCode();
    }
}

List<Person> people = new List<Person>
{
    new Person { Id = 1, Name = "John" },
    new Person { Id = 2, Name = "Jane" },
    new Person { Id = 3, Name = "John" },
    new Person { Id = 4, Name = "Peter" },
    new Person { Id = 5, Name = "Jane" }
};

var uniquePeople = people.Distinct(new PersonNameEqualityComparer());

foreach (var person in uniquePeople)
{
    Console.WriteLine(person.Name);
}

Output:

John
Jane
Peter

Explanation: In the above example, we have a collection of Person objects with duplicate elements. We define a custom equality comparer called PersonNameEqualityComparer that compares the elements based on the Person’s Name property. We apply the Distinct method to the collection and pass the PersonNameEqualityComparer as the custom equality comparer. The Distinct method compares the elements in the collection using the custom equality comparer and returns only the unique elements based on the Person’s Name property. Finally, we iterate through the uniquePeople collection using a foreach loop and print the Name property of each Person to the console.

Conclusion:

The Distinct method is a useful feature of LINQ that allows developers to retrieve unique elements from a collection based on their default equality comparer or a custom equality comparer. It is a powerful tool that can help simplify the code and reduce the number of lines required to perform complex operations on collections. By understanding how to use the Distinct method in C#, developers can improve their skills and productivity in working with collections in their projects.