Overview
Projects each element of a sequence to an IEnumerable<TOut>
and flattens the resulting sequences into one sequence.
This method is used to transform a sequence of collections into a single, flattened sequence.
Syntax
Extension Method
public static IEnumerable<TOut> SelectMany<TSource, TCollection, TResult>( this IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> selector, Func<TSource, TCollection, TResult> resultSelector );
Extension Method (Overload)
public static IEnumerable<TResult> SelectMany<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector );
Extension Method (Overload)
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>( this IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector );
Parameters
source
- An
IEnumerable<TSource>
whose elements to project and flatten. selector
(Overload 1 & 3)- A transform function to apply to each element of
source
. Each function invocation returns anIEnumerable<TCollection>
orIEnumerable<TResult>
. resultSelector
(Overload 1 & 3)- A transform function to produce a result element from an element from
source
and an element from the projectedIEnumerable<TCollection>
.
Return Value
An IEnumerable<TResult>
whose elements are the result of invoking the transform functions on each element of the source sequence and flattening the results.
Exceptions
ArgumentNullException
:source
or one of the selector functions is null.
Remarks
The SelectMany
method projects each element of a sequence to an IEnumerable<T>
and then flattens the resulting sequences into one sequence.
The SelectMany
method is a powerful tool for working with nested collections. It can be used to:
- Flatten a list of lists into a single list.
- Extract all elements from a collection of collections.
- Create a Cartesian product of two sequences.
The overload taking a resultSelector
function allows you to combine elements from the source sequence with elements from the projected collections to form the final result.
Examples
Example 1: Flattening a list of lists
var numbers = new List<int[]> { new [] { 1, 2, 3 }, new [] { 4, 5 }, new [] { 6, 7, 8, 9 } }; var flattenedNumbers = numbers.SelectMany(arr => arr); // flattenedNumbers will contain { 1, 2, 3, 4, 5, 6, 7, 8, 9 }
Example 2: Using SelectMany with a result selector
class Person { public string Name { get; set; } public List<string> Hobbies { get; set; } } var people = new List<Person> { new Person { Name = "Alice", Hobbies = new List<string> { "reading", "hiking" } }, new Person { Name = "Bob", Hobbies = new List<string> { "gaming", "coding" } } }; var personHobbies = people.SelectMany( person => person.Hobbies, (person, hobby) => new { person.Name, Hobby = hobby } ); // personHobbies will contain: // { { Name = "Alice", Hobby = "reading" }, { Name = "Alice", Hobby = "hiking" }, // { Name = "Bob", Hobby = "gaming" }, { Name = "Bob", Hobby = "coding" } }