C# > Advanced C# > LINQ > Ordering and Grouping
LINQ Ordering and Grouping with Multiple Criteria
This snippet demonstrates how to use LINQ to order and group data based on multiple criteria. It showcases ordering by one property in ascending order and another in descending order, followed by grouping based on a specific property. This approach is commonly used when dealing with complex datasets and requiring specific ordering and aggregation.
Code Example
The code initializes a list of `Product` objects, each containing a category, name, and price. The core of the example is the LINQ query that first orders the products by category in ascending order using `OrderBy(p => p.Category)`. Then, it orders the products within each category by price in descending order using `ThenByDescending(p => p.Price)`. Finally, it groups the ordered products by category using `GroupBy(p => p.Category)`. The results are then iterated and printed to the console, displaying the products within each category, ordered by price.
using System;
using System.Collections.Generic;
using System.Linq;
public class Product
{
public string Category { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public class Example
{
public static void Main(string[] args)
{
List<Product> products = new List<Product>
{
new Product { Category = "Electronics", Name = "Laptop", Price = 1200.00m },
new Product { Category = "Clothing", Name = "Shirt", Price = 30.00m },
new Product { Category = "Electronics", Name = "Smartphone", Price = 800.00m },
new Product { Category = "Clothing", Name = "Pants", Price = 60.00m },
new Product { Category = "Electronics", Name = "Tablet", Price = 300.00m },
new Product { Category = "Clothing", Name = "Dress", Price = 90.00m }
};
// Order by Category (ascending) then by Price (descending), then group by Category
var groupedProducts = products.OrderBy(p => p.Category).ThenByDescending(p => p.Price)
.GroupBy(p => p.Category);
foreach (var group in groupedProducts)
{
Console.WriteLine($"Category: {group.Key}");
foreach (var product in group)
{
Console.WriteLine($" - {product.Name}: ${product.Price}");
}
}
}
}
Concepts Behind the Snippet
This snippet utilizes several core LINQ concepts:
Real-Life Use Case
Imagine an e-commerce platform that needs to display products. This snippet could be used to:
Best Practices
Interview Tip
When discussing LINQ ordering and grouping in an interview, be prepared to explain the difference between `OrderBy` and `ThenBy`, and how they can be used to achieve complex sorting scenarios. Also, be ready to discuss the performance implications of these operations on large datasets, and how to optimize them.
When to Use Them
Use ordering and grouping when you need to:
Memory Footprint
LINQ operations, especially `GroupBy`, can potentially consume significant memory, especially when dealing with large datasets. `GroupBy` typically requires creating temporary collections to store the grouped results. If memory usage is a concern, consider using streaming techniques or database-side grouping to reduce the memory footprint.
Alternatives
Alternatives to LINQ ordering and grouping include:
Pros
Cons
FAQ
-
What is the difference between `OrderBy` and `ThenBy`?
`OrderBy` is used to perform the initial sorting of a collection based on a specified criteria. `ThenBy` is used to perform subsequent sorting within the already sorted collection, based on additional criteria. You can chain multiple `ThenBy` calls to sort by multiple levels. -
How does `GroupBy` work?
`GroupBy` divides a collection into groups based on a common key. It returns a collection of `IGrouping` objects, where `TKey` is the type of the key used for grouping, and `TElement` is the type of the elements in the collection. Each `IGrouping` represents a group of elements that share the same key.