Implementing Distinct Operations by Class Properties with LINQ

Nov 27, 2025 · Programming · 11 views · 7.8

Keywords: LINQ | Distinct Operations | C# Programming

Abstract: This article provides an in-depth exploration of using LINQ to perform distinct operations on collections based on class properties in C#. Through detailed analysis of the combination of standard LINQ methods GroupBy and Select, as well as the implementation of custom comparers, it thoroughly explains how to efficiently handle object collections with duplicate identifiers. The article includes complete code examples and performance analysis to help developers understand the applicable scenarios and implementation principles of different methods.

Introduction

In software development, there is often a need to process collections containing duplicate data. Particularly in object-oriented programming, objects may have identical identifying properties while differing in other attributes. This article will use the car class as an example to provide a detailed explanation of how to use LINQ to perform distinct operations on collections based on specific properties.

Problem Scenario Analysis

Assume we have a collection of cars where each car is uniquely identified by its CarCode property. When the collection contains multiple car objects with the same CarCode, we need to filter out cars with unique identifiers.

Core Solution

Using the combination of LINQ's GroupBy and Select methods is an effective way to implement distinct operations based on properties. This method groups cars with the same CarCode through grouping operations, then selects the first element from each group.

List<Car> distinctCars = cars
    .GroupBy(car => car.CarCode)
    .Select(group => group.First())
    .ToList();

Implementation Principle Detailed Explanation

The GroupBy method groups elements in the sequence according to the specified key selector function. For the car collection, it creates a grouped collection with CarCode as the key. Subsequently, the Select method picks the first element from each group, ensuring that only one car object is retained for each CarCode.

Custom Comparator Method

In addition to the grouping method, the Distinct method can also be used by implementing a custom IEqualityComparer<T>. This approach requires creating a specialized comparator for the car class:

public class CarComparer : IEqualityComparer<Car>
{
    public bool Equals(Car x, Car y)
    {
        if (ReferenceEquals(x, y)) return true;
        if (ReferenceEquals(x, null) || ReferenceEquals(y, null)) return false;
        return x.CarCode == y.CarCode;
    }

    public int GetHashCode(Car car)
    {
        return car.CarCode?.GetHashCode() ?? 0;
    }
}

Using the custom comparator:

IEnumerable<Car> distinctCars = cars.Distinct(new CarComparer());

Third-Party Library Solution

The MoreLINQ library provides the DistinctBy extension method, which can more concisely implement distinct operations based on properties:

IEnumerable<Car> distinctCars = cars.DistinctBy(car => car.CarCode);

Performance Comparison and Analysis

The GroupBy method has a time complexity of O(n) and is suitable for most scenarios. The custom comparator method is more efficient in memory usage but requires additional coding effort. The third-party library method offers the best development experience but adds external dependencies.

Practical Application Recommendations

When choosing a distinct method, consider the specific requirements of the project:

Conclusion

Through the detailed analysis in this article, we can see that LINQ provides multiple flexible ways to implement distinct operations based on class properties. Developers can choose the most suitable method according to specific scenarios, balancing code simplicity, performance, and maintainability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.