Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

Enumerable.SelectMany: practical reference

Enumerable.SelectMany: practical reference: The deferred projection calls a collection selector and flattens the returned sequences into one result.

Version: .NET LINQ; verified on .NET 10.0.14

Syntax

source.SelectMany(/* overload-specific arguments */)

The deferred projection calls a collection selector and flattens the returned sequences into one result.

Use it to expand one-to-many relations or nested collections.

Outer and inner order is retained and inner sources are enumerated in sequence. Returning null instead of a sequence causes failure.

An empty source or only empty inner sequences yields no result. Null required selectors cause ArgumentNullException.

The complete program actually uses Enumerable.SelectMany; its exact expected stdout is 1,2,3.

Examples

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

Console.WriteLine(string.Join(",", new[] { new[] { 1, 2 }, new[] { 3 } }.SelectMany(values => values)));

Result

1,2,3

The complete program actually uses `Enumerable.SelectMany`; its exact expected stdout is `1,2,3`.

Sources