Read the syntax
Turn many inner sequences into one sequence and optionally retain their outer context.
SelectMany visits its values as part of one flattened sequence.
What it is and why to use it
A course contains lessons, and a list of courses therefore contains lists within a list. Select projects each outer value to one result, which can still be another sequence. SelectMany flattens the selected inner sequences into one stream of results. This is useful for nested tags, lessons or other one-to-many relationships.
How it works
A collection selector chooses the inner sequence for each outer element. A result-selector overload combines the outer element and each inner element. For LINQ to Objects this is deferred: the selectors and traversal run when the result is enumerated. Empty inner sequences contribute no items. A null inner sequence is not automatically treated as empty.
Common mistakes and limitations
Do not assume Select and SelectMany produce the same shape. Do not hide side effects in selectors or enumerate an expensive source repeatedly without deciding whether to materialise it. Remote IQueryable providers can translate expressions differently; these examples are in-memory Enumerable operations.
Run the examples locally
These complete Program.cs examples use a .NET 10 console project with the C# language version set to 12.0, nullable checking enabled and implicit usings disabled. They use standard .NET libraries and need no extra NuGet packages. This is the verified environment, not a claim that every API requires .NET 10 or works unchanged in .NET Framework, Unity or other runtimes.
Install a supported .NET 10 SDK, create the console project, replace its project file with the settings below, then replace Program.cs with one complete example at a time. Build and run from the project folder. using directives bring namespaces into scope; top-level statements form the program’s entry point, and any helper types follow those statements. Console.WriteLine writes terminal output. Do not combine examples that declare the same type names.
C# in Codectionary’s Playground is editing-only: it does not compile or execute these programs. Run them with the local .NET SDK to reproduce the expected output. Async examples use controlled in-process operations rather than live network services or timing-sensitive delays.
Create and enter a project
dotnet new console -n CSharpExample --framework net10.0
cd CSharpExampleCSharpExample.csproj settings used for these examples
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<LangVersion>12.0</LangVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
</PropertyGroup>
</Project>Build and run after saving one example in Program.cs
dotnet build
dotnet run --no-buildContinue learning
Change one input and predict the result before running it. Follow the related guides to connect this topic with the surrounding language features.
Syntax
var all = groups.SelectMany(group => group.Items);Examples
Flatten arrays of lesson names
Produce one list from several independent groups.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
string[][] groups = { new[] { "Arrays", "Functions" }, Array.Empty<string>(), new[] { "Classes" } };
var names = groups.SelectMany(group => group);
Console.WriteLine(string.Join(", ", names));
- The selector returns each inner array.
- SelectMany visits its values as part of one flattened sequence.
- The empty array adds no values. The traversal order here follows the outer arrays and each inner array.
- Expected output: Arrays, Functions, Classes
- Choose the intended result shape before projecting.
Keep each lesson’s course label
Use the result-selector overload instead of losing outer context.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
var courses = new[] {
(Name: "PHP", Lessons: new[] { "Arrays", "Classes" }),
(Name: "C#", Lessons: new[] { "LINQ" })
};
var lines = courses.SelectMany(course => course.Lessons,
(course, lesson) => $"{course.Name}: {lesson}");
foreach (var line in lines) Console.WriteLine(line);
- The collection selector retrieves the lessons for one course.
- The result selector receives both that course and each lesson.
- The flattened result can therefore label every lesson without a second lookup.
- Expected output: PHP: Arrays PHP: Classes C#: LINQ
- Choose the intended result shape before projecting.
Best practices
- Choose the intended result shape before projecting.
- Handle missing inner collections explicitly.
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference