C# Reference ยท LINQ
Enumerable.Select() method
Reference for C# LINQ Select(): transform each element, project to anonymous types, tuples and records, use the index overload and avoid common deferred-execution traps.
Select projects each element of a sequence into a new form. It always returns exactly one output element per input element, in the same order. Typical uses: extract a property, convert a type, build a new object from several fields, or add a calculated value.
Like Where, Select is lazy: the selector runs while you enumerate the result, not when you call Select. If you need a flat list from nested sequences, use SelectMany instead.
string[] words = ["ab", "cde", "f"]; var lengths = words.Select(w => w.Length); // 2, 3, 1
Try it
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.
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
public static IEnumerable<TResult> Select<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, TResult> selector); public static IEnumerable<TResult> Select<TSource, TResult>( this IEnumerable<TSource> source, Func<TSource, int, TResult> selector);
| Name | Description |
|---|---|
source |
The sequence to transform. |
selector |
Function applied to each element; the second overload also receives the zero-based index. |
Returns: a lazy IEnumerable<TResult> with one value per source element, in source order.
Errors: ArgumentNullException for a null source or selector (immediately). Exceptions inside the selector appear during enumeration.
Usage notes
- The query form is
from x in source select expression. - The output has the same number of elements as the input. To drop elements as well, add
Where; to flatten, useSelectMany. - A projection to an anonymous type is perfect for local use, but it cannot be returned from a method with a precise type. Use a record or tuple then.
- Beware of captured loop variables or objects that change: the selector sees values at enumeration time, not when you built the query.
- Do not use
Selectfor side effects (Select(x => { Print(x); return x; })) and then forget to enumerate; useforeach. - With method groups (
Select(int.Parse)) the compiler may pick an overload with an unexpected parameter, such as one taking an index. Use a lambda when in doubt. - With
IQueryable<T>(databases) the selector becomes SQL columns, so selecting just the fields you need transfers less data. - Reuse the result via
ToList()if you iterate it more than once and the selector is expensive.
Related
- Where()
- SelectMany()
- ToDictionary()
- Anonymous types
- LINQ operators in the tutorial