.NET API Documentation

System.Linq.SelectMany

Methods

Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.

public static IEnumerable<TResult> SelectMany<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TResult>> selector)

Parameters

Name Type Description
source IEnumerable<TSource> An IEnumerable<TSource> whose elements to project and flatten.
selector Func<TSource, IEnumerable<TResult>> A transform function to apply to each element of source; the return value of this function is projected to a new sequence.
public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(this IEnumerable<TSource> source, Func<TSource, IEnumerable<TCollection>> collectionSelector, Func<TSource, TCollection, TResult> resultSelector)

Parameters

Name Type Description
source IEnumerable<TSource> An IEnumerable<TSource> whose elements to project.
collectionSelector Func<TSource, IEnumerable<TCollection>> A transform function to apply to each element of source; theumerable of elements to project and flatten.
resultSelector Func<TSource, TCollection, TResult> A transform function to cross apply the elements from source and the elements from the projected IEnumerable<TCollection>.

Example

The following code example demonstrates how to use the SelectMany method to flatten a sequence of arrays into a single sequence.


using System;
using System.Collections.Generic;
using System.Linq;

public class SelectManyExample
{
    public static void Main(string[] args)
    {
        // A sequence of arrays
        int[][] numbers = new int[][] {
            new int[] { 1, 2, 3 },
            new int[] { 4, 5 },
            new int[] { 6, 7, 8, 9 }
        };

        // Use SelectMany to flatten the arrays into a single sequence
        IEnumerable<int> flattenedNumbers = numbers.SelectMany(arr => arr);

        Console.WriteLine("Flattened sequence:");
        foreach (int number in flattenedNumbers)
        {
            Console.Write(number + " ");
        }
        // Output: 1 2 3 4 5 6 7 8 9

        Console.WriteLine("\n---");

        // Example with result selector
        string[][] words = new string[][]
        {
            new string[] { "apple", "banana" },
            new string[] { "cherry", "date", "elderberry" }
        };

        // Combine words with their lengths
        IEnumerable<string> combined = words.SelectMany(
            arr => arr, // collectionSelector: project each array into its elements
            (arrElement, word) => $"{word} (length: {word.Length})" // resultSelector: create a new string
        );

        Console.WriteLine("Combined words with lengths:");
        foreach (string item in combined)
        {
            Console.WriteLine(item);
        }
        /* Output:
           apple (length: 5)
           banana (length: 6)
           cherry (length: 6)
           date (length: 4)
           elderberry (length: 10)
        */
    }
}
                        

Remarks

The SelectMany extension method is a core part of Language Integrated Query (LINQ). It is commonly used to transform a sequence of collections into a single, flattened sequence. This is useful when you have a collection of collections (e.g., a list of lists, an array of arrays) and you want to operate on all individual elements as if they were in a single list.

The two overloads provide flexibility: the simpler overload projects each element to an IEnumerable<TResult> and then flattens these enumerables. The more complex overload also takes a resultSelector function, allowing you to combine elements from the original source and the projected collection into a new result type.

SelectMany is the LINQ equivalent of a SQL CROSS APPLY or OUTER APPLY operation.

Related Articles