Complete Guide to Selecting Multiple Fields with DISTINCT and ORDERBY in LINQ

Nov 11, 2025 · Programming · 13 views · 7.8

Keywords: LINQ | C# | Anonymous Types | Multi-field Selection | Data Deduplication

Abstract: This article provides an in-depth exploration of selecting multiple fields, performing DISTINCT operations, and applying ORDERBY sorting in C# LINQ. Through analysis of core concepts such as anonymous types and GroupBy operators, it offers multiple implementation solutions and discusses the impact of different data structures on query efficiency. The article includes detailed code examples and performance analysis to help developers master efficient LINQ query techniques.

Fundamentals of Multi-Field Selection in LINQ

Selecting a single field in C# LINQ queries is relatively straightforward, but when multiple fields need to be selected simultaneously, specific technical approaches are required. Anonymous types provide an ideal solution for this scenario, allowing the creation of strongly-typed temporary data structures at compile time.

Application of Anonymous Types

Anonymous types, introduced in C# 3.0, are a crucial feature that enables the creation of objects containing multiple properties without the need for pre-defined classes. In LINQ queries, the new { } syntax can be used to easily select multiple fields:

var result = listObject
    .Select(i => new { i.category_id, i.category_name })
    .Distinct()
    .OrderByDescending(i => i.category_name)
    .ToArray();

This approach creates an array of anonymous types containing both category_id and category_name properties. The compiler automatically generates Equals and GetHashCode methods for this anonymous type, enabling the Distinct operation to function correctly.

Alternative Approach Using GroupBy Operator

When more complex deduplication logic is required, the GroupBy operator provides an alternative solution:

Data[] result = listObject
    .GroupBy(i => new { i.category_id, i.category_name })
    .OrderByDescending(g => g.Key.category_name)
    .Select(g => g.First())
    .ToArray();

This method first groups the data by category_id and category_name, then selects the first element from each group. Although the code is slightly longer, it offers better flexibility in certain scenarios.

Data Structure Selection and Optimization

The struct used in the original problem may not be the optimal choice in some situations. If extensive LINQ query operations are required, consider the following alternatives:

Performance Considerations and Best Practices

When selecting methods for multi-field queries, performance factors should be considered:

Extended Application Scenarios

The SelectMany technique mentioned in the reference article can be applied to more complex scenarios, such as combining values from multiple fields into a single collection:

var allValues = listObject
    .Select(i => new List<object>() { i.category_id, i.category_name })
    .SelectMany(item => item)
    .Distinct();

While this approach is not directly applicable to the current problem, it demonstrates the powerful capabilities of LINQ when handling complex data structures.

Conclusion

By appropriately using anonymous types and suitable combinations of LINQ operators, efficient multi-field selection, deduplication, and sorting operations can be achieved. Developers should choose the most suitable method based on specific requirements and consider data structure and performance optimization.

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.