C# LINQ Examples
C# Basic
C# Data Type
C# OOP
C# IO
C# Graphics & UI
C# Advanced
GroupBy
is a LINQ operator that allows you to group elements in a collection based on a key selector function. Here's an example of using GroupBy
in C#:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; var groups = numbers.GroupBy(n => n % 3); foreach (var group in groups) { Console.WriteLine($"Group {group.Key}: {string.Join(",", group)}"); }
In this example, the numbers
list is grouped based on the remainder of each element when divided by 3. The resulting groups are then printed to the console.
You can also use GroupBy
and OrderBy
together to group elements and sort them within each group. Here's an example:
List<int> numbers = new List<int> { 10, 2, 7, 4, 5, 8, 1, 6, 9, 3 }; var groups = numbers.GroupBy(n => n % 3) .OrderBy(g => g.Key); foreach (var group in groups) { Console.WriteLine($"Group {group.Key}: {string.Join(",", group.OrderBy(n => n))}"); }
In this example, the numbers
list is first grouped based on the remainder of each element when divided by 3, and then the resulting groups are sorted by their keys. Within each group, the elements are also sorted in ascending order before being printed to the console.
Finally, you can use GroupBy
together with Count()
in a having
clause to filter groups based on the number of elements they contain. Here's an example:
List<string> words = new List<string> { "apple", "banana", "cherry", "date", "elderberry", "fig" }; var groups = words.GroupBy(w => w[0]) .Where(g => g.Count() >= 2); foreach (var group in groups) { Console.WriteLine($"Group {group.Key}: {string.Join(",", group)}"); }
In this example, the words
list is first grouped based on the first letter of each word, and then the resulting groups are filtered to include only those that have at least two elements. The resulting groups are then printed to the console.
C# LINQ GroupBy example:
var numbers = new List<int> { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }; var groupedNumbers = numbers.GroupBy(num => num); foreach (var group in groupedNumbers) { Console.WriteLine($"Key: {group.Key}, Count: {group.Count()}"); }
LINQ GroupBy multiple columns C#:
var persons = new List<Person> { /* List of Person objects */ }; var groupedPersons = persons.GroupBy(person => new { person.City, person.Age }); foreach (var group in groupedPersons) { Console.WriteLine($"City: {group.Key.City}, Age: {group.Key.Age}, Count: {group.Count()}"); }
C# LINQ GroupBy and Sum:
var sales = new List<Sale> { /* List of Sale objects */ }; var totalSalesByProduct = sales.GroupBy(sale => sale.ProductId, (key, group) => new { ProductId = key, TotalSales = group.Sum(s => s.Amount) });
GroupBy and OrderBy in LINQ C#:
var numbers = new List<int> { 3, 1, 4, 1, 5, 9, 2, 6, 5 }; var orderedGroups = numbers.GroupBy(num => num).OrderBy(group => group.Key); foreach (var group in orderedGroups) { Console.WriteLine($"Key: {group.Key}, Count: {group.Count()}"); }
C# LINQ GroupBy and Count distinct:
var numbers = new List<int> { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4 }; var distinctCounts = numbers.GroupBy(num => num).Count(); Console.WriteLine($"Distinct Counts: {distinctCounts}");
C# LINQ GroupBy and Max:
var numbers = new List<int> { 10, 5, 8, 15, 3, 12 }; var maxNumbers = numbers.GroupBy(num => "Max", (key, group) => group.Max()); Console.WriteLine($"Max Numbers: {string.Join(", ", maxNumbers)}");
C# LINQ GroupBy and Average:
var numbers = new List<int> { 10, 20, 30, 40, 50 }; var averageByGroup = numbers.GroupBy(num => "All", (key, group) => group.Average()); Console.WriteLine($"Average: {averageByGroup.First()}");
GroupBy with multiple keys in LINQ C#:
var persons = new List<Person> { /* List of Person objects */ }; var groupedPersons = persons.GroupBy(person => new { person.City, person.Age }); foreach (var group in groupedPersons) { Console.WriteLine($"City: {group.Key.City}, Age: {group.Key.Age}, Count: {group.Count()}"); }
C# LINQ GroupBy and Sum with condition:
var sales = new List<Sale> { /* List of Sale objects */ }; var totalSalesByProduct = sales.GroupBy(sale => sale.ProductId, (key, group) => new { ProductId = key, TotalSales = group.Sum(s => s.Amount) });