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# Linq SelectMany Method
The SelectMany method in C# LINQ is used to flatten collections of collections into a single sequence. It projects each element of a sequence to an IEnumerable<T> and then flattens the resulting sequences into one sequence.
Syntax
Following is the basic syntax for SelectMany method −
public static IEnumerable<TResult> SelectMany<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, IEnumerable<TResult>> selector
)
Parameters
source − The input sequence to transform.
selector − A function that transforms each element into a collection.
Return Value
Returns an IEnumerable<T> whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.
Using SelectMany to Convert Strings to Characters
A common use case is converting an array of strings into individual characters −
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
string[] str = { "Mobile", "Laptop", "Tablet" };
var res = str.SelectMany(item => item.ToCharArray());
Console.WriteLine("String converted to character array: ");
foreach (char letter in res) {
Console.Write(letter + " ");
}
Console.WriteLine();
Console.WriteLine("\nTotal characters: " + res.Count());
}
}
The output of the above code is −
String converted to character array: M o b i l e L a p t o p T a b l e t Total characters: 18
Using SelectMany with Nested Collections
Here's how SelectMany flattens collections of collections −
using System;
using System.Linq;
using System.Collections.Generic;
public class School {
public string Name { get; set; }
public List<string> Students { get; set; }
}
public class Demo {
public static void Main() {
var schools = new List<School> {
new School {
Name = "School A",
Students = new List<string> { "Alice", "Bob" }
},
new School {
Name = "School B",
Students = new List<string> { "Charlie", "David", "Eve" }
}
};
var allStudents = schools.SelectMany(school => school.Students);
Console.WriteLine("All students from all schools:");
foreach (string student in allStudents) {
Console.WriteLine(student);
}
}
}
The output of the above code is −
All students from all schools: Alice Bob Charlie David Eve
SelectMany vs Select
Understanding the difference between Select and SelectMany −
| Select | SelectMany |
|---|---|
| Projects each element to a new form (1-to-1 mapping). | Flattens collections into a single sequence (1-to-many mapping). |
Returns IEnumerable<IEnumerable<T>> for collections. |
Returns IEnumerable<T> by flattening. |
| Maintains the original structure. | Removes one level of nesting. |
Example Comparing Select and SelectMany
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
string[] words = { "Hello", "World" };
// Using Select - returns IEnumerable<char[]>
var selectResult = words.Select(word => word.ToCharArray());
Console.WriteLine("Select result type: IEnumerable<char[]>");
Console.WriteLine("Count of arrays: " + selectResult.Count());
// Using SelectMany - returns IEnumerable<char>
var selectManyResult = words.SelectMany(word => word.ToCharArray());
Console.WriteLine("\nSelectMany result type: IEnumerable<char>");
Console.WriteLine("Count of characters: " + selectManyResult.Count());
Console.Write("Characters: ");
foreach (char c in selectManyResult) {
Console.Write(c + " ");
}
}
}
The output of the above code is −
Select result type: IEnumerable<char[]> Count of arrays: 2 SelectMany result type: IEnumerable<char> Count of characters: 10 Characters: H e l l o W o r l d
Conclusion
The SelectMany method is essential for flattening nested collections into a single sequence. It's particularly useful when working with collections of collections, allowing you to perform operations on all elements across multiple levels of nesting in a streamlined way.
