Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
GroupBy() Method in C#
The GroupBy() method in C# is a LINQ extension method that groups elements from a collection based on a specified key selector function. It returns an IGrouping<TKey, TElement> where elements sharing the same key are grouped together.
Syntax
Following is the syntax for the GroupBy() method −
public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector
)
Parameters
source ? The collection to group elements from.
keySelector ? A function that extracts the key for each element.
Return Value
Returns an IEnumerable<IGrouping<TKey, TSource>> where each group contains elements that share the same key.
Using GroupBy() with Boolean Keys
The following example groups numbers based on whether they are smaller than or equal to 50 −
using System;
using System.Linq;
class Demo {
static void Main() {
int[] arr = { 2, 30, 45, 60, 70 };
var check = arr.GroupBy(b => chkSmaller(b));
foreach (var val in check) {
Console.WriteLine("Key: " + val.Key);
foreach (var res in val) {
Console.WriteLine(" " + res);
}
}
}
static bool chkSmaller(int a) {
return a <= 50;
}
}
The output of the above code is −
Key: True 2 30 45 Key: False 60 70
Using GroupBy() with String Keys
The following example groups words by their first letter −
using System;
using System.Linq;
class Program {
static void Main() {
string[] words = { "apple", "banana", "cherry", "avocado", "blueberry" };
var grouped = words.GroupBy(word => word[0]);
foreach (var group in grouped) {
Console.WriteLine("Starting with '" + group.Key + "':");
foreach (var word in group) {
Console.WriteLine(" " + word);
}
}
}
}
The output of the above code is −
Starting with 'a': apple avocado Starting with 'b': banana blueberry Starting with 'c': cherry
Using GroupBy() with Complex Objects
The following example groups employees by their department −
using System;
using System.Linq;
class Employee {
public string Name { get; set; }
public string Department { get; set; }
public Employee(string name, string dept) {
Name = name;
Department = dept;
}
}
class Program {
static void Main() {
Employee[] employees = {
new Employee("John", "IT"),
new Employee("Alice", "HR"),
new Employee("Bob", "IT"),
new Employee("Carol", "HR")
};
var grouped = employees.GroupBy(emp => emp.Department);
foreach (var group in grouped) {
Console.WriteLine("Department: " + group.Key);
foreach (var emp in group) {
Console.WriteLine(" " + emp.Name);
}
}
}
}
The output of the above code is −
Department: IT John Bob Department: HR Alice Carol
Conclusion
The GroupBy() method is a powerful LINQ extension that groups collection elements by a specified key. It's useful for organizing data, performing aggregate operations on groups, and creating hierarchical data structures from flat collections.
