Enumerable.Select()
A LINQ extension method that transforms each element of a sequence to produce a new sequence. This page also covers SelectMany(), which flattens nested sequences.
Syntax
using System.Linq; // Transforms each element and returns a new sequence. IEnumerable<TResult> result = source.Select(x => transformExpression); // Flattens a nested sequence by one level. IEnumerable<TResult> flat = source.SelectMany(x => x.InnerCollection); // Flattens a nested sequence and transforms each element using both parent and child. IEnumerable<TResult> flat2 = source.SelectMany(x => x.Items, (parent, child) => transformExpression);
Method List
| Method | Description |
|---|---|
| Select(selector) | Transforms each element and returns a new sequence. |
| Select((x, i) => ...) | Transforms each element using both the element and its index. |
| SelectMany(collectionSelector) | Flattens a nested collection by one level. |
| SelectMany(collectionSelector, resultSelector) | Flattens a nested collection and transforms each element. |
Sample Code
using System;
using System.Collections.Generic;
using System.Linq;
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
// Multiplies each element by 2.
IEnumerable<int> doubled = numbers.Select(n => n * 2);
Console.WriteLine(string.Join(", ", doubled)); // 2, 4, 6, 8, 10
// Converts each number to a string.
IEnumerable<string> strs = numbers.Select(n => $"No.{n}");
Console.WriteLine(string.Join(", ", strs)); // No.1, No.2, No.3, No.4, No.5
// Transforms each element into a string that includes its index.
IEnumerable<string> indexed = numbers.Select((n, i) => $"[{i}]={n}");
Console.WriteLine(string.Join(", ", indexed)); // [0]=1, [1]=2, [2]=3, [3]=4, [4]=5
// Projects each number into an anonymous type with two properties.
var items = numbers.Select(n => new { Value = n, Square = n * n });
foreach (var item in items)
Console.WriteLine($"Value:{item.Value}, Square:{item.Square}");
// Uses SelectMany to flatten a nested list.
List<List<int>> nested = new List<List<int>>
{
new List<int> { 1, 2 },
new List<int> { 3, 4 },
new List<int> { 5 },
};
IEnumerable<int> flat = nested.SelectMany(x => x);
Console.WriteLine(string.Join(", ", flat)); // 1, 2, 3, 4, 5
// Flattens each string in a list into its individual characters.
List<string> words = new List<string> { "abc", "de" };
IEnumerable<char> chars = words.SelectMany(w => w);
Console.WriteLine(string.Join(", ", chars)); // a, b, c, d, e
Notes
Select() is the equivalent of map in other languages — it transforms each element one-to-one. You can also change the return type, and by projecting into an anonymous type you can produce objects with multiple properties.
SelectMany() performs a many-to-one (fan-out) transformation: it extracts the inner collection from each element and merges everything into a single flat sequence. Using Select() on a nested collection produces a sequence of sequences, so use SelectMany() whenever you need to flatten the result.
To transform only elements that match a condition, combine this method with Enumerable.Where(). To sort the transformed results, see Enumerable.OrderBy().
If you find any errors or copyright issues, please contact us.