ZetCode

C# LINQ Select

last modified November 6, 2026

In this article we show how to map elements to an expression with LINQ's Select method and the select clause.

Language-Integrated Query (LINQ) is a domain-specific language for querying data from various data sources, including arrays, lists, XML files, or databases.

The select clause or the Select method projects each element of a sequence into a new form. It selects, projects and transforms elements in a collection. The operation is usually called map in other languages.

The Enumerable.Select method takes a selector function as a parameter. The selector is a transform function which is applied to each source element. The result has the same number of elements as the source sequence.

C# LINQ Select simple example

The first example is a simple program which uses LINQ Select.

Program.cs
List<int> vals = [1, 2, 3, 4, 5, 6];

var res = vals.Select(e => e * 2);
Console.WriteLine(string.Join(',', res));

The program defines a list of integers. All elements are multiplied by two.

var res = vals.Select(e => e * 2);

The Select method applies the given lambda expression on each of the elements. It returns a sequence of values modified by the selector function. The source list is not changed.

$ dotnet run
2,4,6,8,10,12

C# LINQ Func selector

The selector is a Func<TSource, TResult> delegate. Func is a built-in generic delegate type. It can be created from a method, an anonymous method or a lambda expression.

Program.cs
List<int> vals = [1, 2, 3, 4, 5, 6];

Func<int, int> byfive = e => e * 5;
var res = vals.Select(byfive);
Console.WriteLine(string.Join(',', res));

List<string> words = ["sky", "cup", "loud"];

var upper = words.Select(Capitalize);
Console.WriteLine(string.Join(',', upper));

static string Capitalize(string s) => char.ToUpper(s[0]) + s[1..];

The program applies a Func delegate and a static local function on the elements of two lists.

var upper = words.Select(Capitalize);

A method group can be passed directly to Select; we do not have to wrap it in a lambda expression.

$ dotnet run
5,10,15,20,25,30
Sky,Cup,Loud

C# LINQ select clause

Query expressions are an alternative LINQ syntax. In a query expression, we use the select clause. The compiler translates the query into a call of the Select method.

Program.cs
List<string> words = ["sky", "cup", "loud", "war", "water"];

var res = from word in words
          select word.Length;

Console.WriteLine(string.Join(',', res));

In the program, we transform a list of words into a sequence of word lengths.

$ dotnet run
3,3,4,3,5

C# LINQ Select changing the element type

The selector can return a value of a different type than the source element.

Program.cs
List<string> words = ["sky", "cup", "loud", "war", "water"];

var lengths = words.Select(e => e.Length);
var chars = words.Select(e => e[0]);
var flags = words.Select(e => e.Length > 3);

Console.WriteLine(string.Join(',', lengths));
Console.WriteLine(string.Join(',', chars));
Console.WriteLine(string.Join(',', flags));

From a list of strings we create a sequence of integers, a sequence of characters, and a sequence of booleans. The type of the resulting IEnumerable<TResult> is inferred from the selector.

$ dotnet run
3,3,4,3,5
s,c,l,w,w
False,False,True,False,True

C# LINQ selector with index

We can pass the index of the element to the selector function.

Program.cs
List<string> words = ["sky", "cup", "loud", "war", "water"];

var res = words.Select((e, idx) => new { idx, len = e.Length });

foreach (var e in res)
{
    Console.WriteLine(e);
}

Console.WriteLine("--------------------");

foreach (var (idx, word) in words.Index())
{
    Console.WriteLine($"{idx}: {word}");
}

In the program, the resulting object also includes the index of the element.

var res = words.Select((e, idx) => new { idx, len = e.Length });

This overload of Select takes a selector with two parameters: the element and its zero-based index.

foreach (var (idx, word) in words.Index())

Since .NET 9, the Index method returns tuples of indexes and elements. It is a shorthand for the indexed Select when we only need to number the elements.

$ dotnet run
{ idx = 0, len = 3 }
{ idx = 1, len = 3 }
{ idx = 2, len = 4 }
{ idx = 3, len = 3 }
{ idx = 4, len = 5 }
--------------------
0: sky
1: cup
2: loud
3: war
4: water

C# LINQ select projection

The select clause also performs projections. A projection is a selection of specific fields from the returned objects.

Program.cs
List<User> users =
[
    new("John", "Doe", 1230),
    new("Lucy", "Novak", 670),
    new("Ben", "Walter", 2050),
    new("Robin", "Brown", 2300),
    new("Amy", "Doe", 1250),
    new("Joe", "Draker", 1190),
    new("Janet", "Doe", 980),
    new("Albert", "Novak", 1930),
];

var res = from user in users
          orderby user.Salary
          select user.Salary;

Console.WriteLine(string.Join(' ', res));

var names = users.Where(u => u.LastName == "Doe")
                 .Select(u => $"{u.FirstName} {u.LastName}");

Console.WriteLine(string.Join(", ", names));

record User(string FirstName, string LastName, int Salary);

We have list of users having three fields: first name, last name, and salary.

var res = from user in users
          orderby user.Salary
          select user.Salary;

We sort the users by salary and pick only the salary field in the resulting sequence.

var names = users.Where(u => u.LastName == "Doe")
                 .Select(u => $"{u.FirstName} {u.LastName}");

Select is often chained after Where. First we filter the users and then we build a full name for each of the remaining ones.

$ dotnet run
670 980 1190 1230 1250 1930 2050 2300
John Doe, Amy Doe, Janet Doe

C# LINQ select to new shapes

The selector can create new objects. We can project into anonymous types, tuples, or our own types.

Program.cs
List<User> users =
[
    new("John", "Doe", 1230),
    new("Lucy", "Novak", 670),
    new("Ben", "Walter", 2050),
];

var anon = users.Select(u => new { u.FirstName, Yearly = u.Salary * 12 });
Console.WriteLine(string.Join('\n', anon));

var tuples = users.Select(u => (Name: u.FirstName, u.Salary));

foreach (var (name, salary) in tuples)
{
    Console.WriteLine($"{name} earns {salary}");
}

var dtos = from u in users
           let full = $"{u.FirstName} {u.LastName}"
           select new UserDto(full, u.Salary > 1000);

Console.WriteLine(string.Join('\n', dtos));

record User(string FirstName, string LastName, int Salary);
record UserDto(string FullName, bool HighEarner);

The users are projected into three different shapes.

var anon = users.Select(u => new { u.FirstName, Yearly = u.Salary * 12 });

An anonymous type is created with the new keyword without a type name. The property names are inferred from the expression or given explicitly. Anonymous types are handy inside a method, but they cannot be returned from it with a meaningful type.

var tuples = users.Select(u => (Name: u.FirstName, u.Salary));

A value tuple is a lightweight alternative. The tuples can be deconstructed in the foreach loop.

var dtos = from u in users
           let full = $"{u.FirstName} {u.LastName}"
           select new UserDto(full, u.Salary > 1000);

The let clause stores an intermediate result which is then used in the select clause. We project the users into instances of the UserDto record.

$ dotnet run
{ FirstName = John, Yearly = 14760 }
{ FirstName = Lucy, Yearly = 8040 }
{ FirstName = Ben, Yearly = 24600 }
John earns 1230
Lucy earns 670
Ben earns 2050
UserDto { FullName = John Doe, HighEarner = True }
UserDto { FullName = Lucy Novak, HighEarner = False }
UserDto { FullName = Ben Walter, HighEarner = True }

C# LINQ SelectMany

When the selector returns a collection for each element, Select produces a sequence of collections. The SelectMany method flattens these collections into a single sequence.

Program.cs
List<List<int>> nested =
[
    [1, 2, 3],
    [4, 5],
    [6, 7, 8, 9]
];

var flat = nested.SelectMany(e => e);
Console.WriteLine(string.Join(',', flat));

List<string> sentences = ["an old falcon", "a blue sky"];

var words = sentences.SelectMany(s => s.Split(' '));
Console.WriteLine(string.Join(',', words));

var pairs = from s in new[] { "S", "M" }
            from c in new[] { "red", "blue" }
            select $"{s}-{c}";

Console.WriteLine(string.Join(',', pairs));

We flatten a nested list, split sentences into words, and combine two sequences.

var words = sentences.SelectMany(s => s.Split(' '));

Each sentence is split into an array of words; SelectMany joins all the arrays into one sequence of words.

var pairs = from s in new[] { "S", "M" }
            from c in new[] { "red", "blue" }
            select $"{s}-{c}";

In a query expression, multiple from clauses are translated into SelectMany. The query produces all combinations of sizes and colours.

$ dotnet run
1,2,3,4,5,6,7,8,9
an,old,falcon,a,blue,sky
S-red,S-blue,M-red,M-blue

C# LINQ Select deferred execution

Select uses deferred execution. The selector is not called when the query is defined, but when the result is enumerated.

Program.cs
List<int> vals = [1, 2, 3];

var res = vals.Select(e =>
{
    Console.WriteLine($"mapping {e}");
    return e * 10;
});

Console.WriteLine("query defined");

vals.Add(4);

foreach (var e in res)
{
    Console.WriteLine(e);
}

List<int> snapshot = res.ToList();
Console.WriteLine(snapshot.Count);

The selector prints a message each time it is called.

vals.Add(4);

We add an element to the list after the query has been defined. Since the query has not run yet, the new element is included in the result.

List<int> snapshot = res.ToList();

Each enumeration runs the selector again. To evaluate the query once and store the results, we call ToList or ToArray.

$ dotnet run
query defined
mapping 1
10
mapping 2
20
mapping 3
30
mapping 4
40
mapping 1
mapping 2
mapping 3
mapping 4
4

Source

Enumerable.Select method

In this article we have presented the LINQ Select operation.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all C# tutorials.