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
C# Program to Split a String Collections into Groups
Welcome to this comprehensive tutorial on creating a C# program to split a collection of strings into groups using Language Integrated Query (LINQ). Whether you're a novice or an intermediate programmer, this guide will provide you with the insights necessary to understand the power of LINQ in C# and its applications in data manipulation.
Understanding the Concept of Grouping in LINQ
Grouping is a powerful concept in data manipulation that involves organizing data into categories based on specified criteria. In LINQ, the GroupBy method is used to group elements in a collection based on a key selector function. The result is a collection of groups where each group contains elements that share a common key.
Syntax
Following is the basic syntax for using GroupBy in LINQ
var groupedData = collection.GroupBy(item => keySelector);
Where keySelector is a function that returns the key for grouping each item in the collection.
Using GroupBy with First Letter
Let's create a program that groups a collection of fruit names by their first letter
using System;
using System.Linq;
using System.Collections.Generic;
public class Program {
public static void Main() {
// Collection of fruit names
List<string> fruits = new List<string>() {
"Apple", "Apricot", "Banana", "Cherry", "Date",
"Elderberry", "Fig", "Grape", "Honeydew"
};
// Use LINQ to group fruits by their first letter
var groupedFruits = fruits.GroupBy(fruit => fruit[0]);
// Display grouped results
foreach (var group in groupedFruits) {
Console.WriteLine($"Group '{group.Key}':");
foreach (var fruit in group) {
Console.WriteLine($" {fruit}");
}
Console.WriteLine();
}
}
}
The output of the above code is
Group 'A': Apple Apricot Group 'B': Banana Group 'C': Cherry Group 'D': Date Group 'E': Elderberry Group 'F': Fig Group 'G': Grape Group 'H': Honeydew
Using GroupBy with String Length
You can also group strings by other criteria, such as their length
using System;
using System.Linq;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<string> words = new List<string>() {
"Cat", "Dog", "Elephant", "Fox", "Giraffe", "Ant"
};
// Group words by their length
var groupedByLength = words.GroupBy(word => word.Length);
foreach (var group in groupedByLength.OrderBy(g => g.Key)) {
Console.WriteLine($"Length {group.Key}:");
foreach (var word in group) {
Console.WriteLine($" {word}");
}
Console.WriteLine();
}
}
}
The output of the above code is
Length 3: Cat Dog Fox Ant Length 7: Giraffe Length 8: Elephant
Using GroupBy with Custom Conditions
For more complex grouping, you can use custom conditions in the key selector
using System;
using System.Linq;
using System.Collections.Generic;
public class Program {
public static void Main() {
List<string> names = new List<string>() {
"Alice", "Bob", "Charlie", "Diana", "Edward", "Fiona"
};
// Group names by vowel or consonant starting letter
var groupedByType = names.GroupBy(name =>
"AEIOU".Contains(char.ToUpper(name[0])) ? "Vowel" : "Consonant"
);
foreach (var group in groupedByType) {
Console.WriteLine($"{group.Key} names:");
foreach (var name in group) {
Console.WriteLine($" {name}");
}
Console.WriteLine();
}
}
}
The output of the above code is
Vowel names: Alice Edward Consonant names: Bob Charlie Diana Fiona
Common Use Cases
-
Data Analysis: Categorizing records for statistical analysis
-
Reporting: Creating summary reports grouped by specific criteria
-
User Interface: Organizing data for display in grouped lists or tables
-
File Organization: Grouping files by extension, size, or creation date
Conclusion
LINQ's GroupBy method provides a powerful way to organize string collections into groups based on any criteria you define. Whether grouping by first letter, length, or custom conditions, GroupBy makes data categorization simple and efficient in C#.
