Home >Backend Development >C++ >Why Doesn't LINQ Distinct() Work as Expected with Custom Objects?

Why Doesn't LINQ Distinct() Work as Expected with Custom Objects?

Susan Sarandon
Susan SarandonOriginal
2025-01-20 19:28:10787browse

Why Doesn't LINQ Distinct() Work as Expected with Custom Objects?

Abnormal behavior of LINQ Distinct() method in custom objects

When using LINQ to Objects, the Distinct() method may not always produce the expected results when working with custom objects. By default, LINQ only considers references for uniqueness comparisons.

In the provided code example, you have a list of Book objects, each of which contains a list of Author objects. When you use SelectMany() to flatten the Authors collection into a single list, and then call Distinct() , you expect it to return only the unique author. However, you observe that the same author (Jon Skeet) appears twice.

The problem is that the Distinct() methods in LINQ operate on object references by default. Since there are two separate Author objects in your list, LINQ will consider them to be different even though they have the same value. To overcome this problem, you need to provide a custom equality implementation that clearly defines how objects are compared for uniqueness.

Override the Equals method

As suggested in your question, you can override the Author method in the Equals() class to specify equality conditions. However, your current implementation always returns true, making all authors appear to be equal.

Implement IEquatable interface

A more appropriate approach is to implement the IEquatable<Author> interface in your class. This interface allows you to define custom equality comparators. In your case you can compare based on the author's name:

<code class="language-csharp">public class Author : IEquatable<Author>
{
    public bool Equals(Author other)
    {
        return FirstName == other.FirstName && LastName == other.LastName;
    }
}</code>

Reuse author in LINQ queries

After implementing IEquatable<Author>, you can use it to improve the behavior of the Distinct() method. Modify your LINQ query as follows:

<code class="language-csharp">var temp = books.SelectMany(book => book.Authors).DistinctBy(a => new { a.FirstName, a.LastName });</code>

By defining tuples of DistinctBy() and FirstName using LastName and a custom lambda expression, you can explicitly specify how authors are compared for uniqueness.

By following these steps, you can ensure that the Distinct() method correctly identifies and eliminates duplicate authors while preserving their values.

The above is the detailed content of Why Doesn't LINQ Distinct() Work as Expected with Custom Objects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn