Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท LINQ

LINQ SelectMany() method

Reference for LINQ SelectMany() in C#: flatten lists of lists, project each child with its parent, build cross joins and avoid common mistakes.

SelectMany() projects every element of a sequence into another sequence and then concatenates all those inner sequences into one flat result. Where Select() produces exactly one output per input, SelectMany() produces zero, one or many.

Typical uses are flattening a list of lists, walking a parent-child relation (orders and their lines), splitting text into words, and building every combination of two sequences. It uses deferred execution: nothing runs until you enumerate the result.

C#
IEnumerable<int> flat = listOfLists.SelectMany(inner => inner);
var pairs = people.SelectMany(p => p.Pets, (p, pet) => new { p.Name, pet });

Try it

C# Flatten and split

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Parents with children and cross products

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

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

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

public static IEnumerable<TResult> SelectMany<TSource, TCollection, TResult>(
    this IEnumerable<TSource> source,
    Func<TSource, IEnumerable<TCollection>> collectionSelector,
    Func<TSource, TCollection, TResult> resultSelector);
Name Description
source The outer sequence.
selector Returns the inner sequence for one source element; the second form also receives the zero-based index of that element.
collectionSelector Like selector, used with a result selector.
resultSelector Combines the outer element with each inner element into the final item.

Returns: a lazily evaluated sequence with all inner elements, in order: everything from the first source element, then the second, and so on.

Errors: ArgumentNullException if source or a selector is null, thrown immediately. If a selector returns null instead of a sequence, a NullReferenceException is thrown while enumerating, so return an empty sequence instead.

Usage notes

  • Query syntax: a second from clause compiles to SelectMany(): from o in orders from l in o.Lines select l. See query syntax.
  • Empty inner sequences vanish. A parent with no children contributes nothing. To keep such parents, use a left-join pattern with DefaultIfEmpty() or GroupJoin().
  • Strings are sequences of char. words.SelectMany(w => w) yields every character, which is occasionally useful and occasionally a surprise.
  • Prefer the result selector over a nested Select() when you need both parent and child; it avoids an extra level of nesting.
  • Cost: O(total number of inner elements). Each inner sequence is enumerated once, in order.
  • Nullable inner collections: use o.Lines ?? [] (a collection expression) to skip parents whose collection is null.
  • Do not use it for joins on keys across two large sources: a cross product compared with a Where() is O(n x m). Join() builds a lookup and is much faster.