---
title: "Why is LINQ distinct not giving distinct results?"  
description: "Why is LINQ distinct not giving distinct results?"  
author: "Sandra Emily"  
published: 2023-08-17  
updated: 2023-08-18  
canonical: https://www.mindstick.com/forum/159566/why-is-linq-distinct-not-giving-distinct-results  
category: "c#"  
tags: ["c#", "linq"]  
reading_time: 3 minutes  

---

# Why is LINQ distinct not giving distinct results?

Why is [LINQ](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries) [distinct](https://www.mindstick.com/forum/159558/why-c-sharp-linq-distinct-doesn-t-work) not [giving](https://yourviews.mindstick.com/view/80639/regifting-is-far-better-than-giving-away-a-bouquet) distinct [results](https://yourviews.mindstick.com/story/4497/usage-of-baking-soda-magical-results)?

## Replies

### Reply by Aryan Kumar

There are a few reasons why the LINQ `Distinct()` method might not be giving distinct results. Here are some of the most common reasons:

- The `Distinct()` method is not being used correctly. The `Distinct()` method takes a sequence as its input and returns a new sequence that contains only the distinct elements from the original sequence. The sequence must be an iterable collection, such as a `List<T>` or an `IEnumerable<T>`.
- The elements in the sequence are not comparable. The `Distinct()` method uses the default equality comparer to compare the elements in the sequence. If the elements are not comparable, the `Distinct()` method will not be able to remove the duplicates.
- The `Distinct()` method is being used with a custom comparer. If you are using a custom comparer with the `Distinct()` method, make sure that the comparer is correctly implemented. The comparer must be able to compare two elements of the same type and return a value indicating whether the two elements are equal.

If you are still having trouble getting the `Distinct()` method to work, you can try using the `IEqualityComparer` interface to provide a custom comparer. The `IEqualityComparer` interface defines two methods: `Equals()` and `GetHashCode()`. The `Equals()` method is used to compare two elements and return a value indicating whether the two elements are equal. The `GetHashCode()` method is used to return a hash code for an element. The hash code is used to quickly compare elements in a hash table.

Here is an example of how to use the `IEqualityComparer` interface to provide a custom comparer for the `Distinct()` method:

C#

```plaintext
public class Person {
    public int Age { get; set; }
}

public class PersonComparer : IEqualityComparer<Person> {
    public bool Equals(Person x, Person y) {
        return x.Age == y.Age;
    }

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

var people = new List<Person>();
people.Add(new Person { Age = 20 });
people.Add(new Person { Age = 30 });
people.Add(new Person { Age = 10 });

var distinctPeople = people.Distinct(new PersonComparer());
```

In this example, the `PersonComparer` class implements the `IEqualityComparer` interface and provides a custom comparer for the `Person` class. The `Equals()` method compares the ages of two `Person` objects and returns a value indicating whether the two objects are equal. The `GetHashCode()` method returns a hash code for a `Person` object.

The `Distinct()` method is a powerful tool for removing duplicates from a sequence. However, it is important to use it correctly to avoid unexpected results.


---

Original Source: https://www.mindstick.com/forum/159566/why-is-linq-distinct-not-giving-distinct-results

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
