Summary: in this tutorial, you’ll learn how to use the LINQ Select()
method to project each element of a sequence into a new form.
Introduction to the LINQ Select method
The Select()
method is a LINQ extension method. The Select()
method projects or transforms each element of a sequence into a new form.
The following shows the syntax of the Select()
method:
IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, int, TResult> selector
)
Code language: C# (cs)
In this syntax:
TSource
represents the element’s type in thesource
sequence.TResult
is the type of value returned by theselector
.source
is an input sequence with the type IEnumerable<TSource>.selector
is a transform function that applies to each element in thesource
sequence. Theselector
has the typeFunc<TSource, TResult>
. The second parameter with the typeint
of the transform function is the index of the element in thesource
sequence.
The Select()
method returns an IEnumerable<T>
whose elements are the result of the transform function on each element of the source
sequence.
If the source
or selector
parameter is null, the Select()
method throws an ArgumentNullException
.
Like other LINQ extension methods, the Select()
method is implemented using deferred execution. It means that the query returned by the Select()
method is not executed until it is enumerated by a foreach
or by calling its GetEnumerator()
method.
LINQ Select() method examples
Let’s take some examples of using the LINQ Select()
method.
1) Using the LINQ Select() method to transform a sequence of integer
The following programs demonstrate how to use the LINQ Select()
method to create a new sequence of numbers that are squares of the original numbers:
using static System.Console;
var numbers = new[] { 1, 5, 3, 4, 7 };
var squares = numbers.Select(n => n * n);
foreach (var square in squares)
{
WriteLine($"{square} ");
}
Code language: C# (cs)
How it works.
First, define an array of five numbers:
var numbers = new[] { 1, 5, 3, 4, 7 };
Code language: C# (cs)
Second, create a new sequence of numbers called squares using the Select()
method.:
var squares = numbers.Select(n => n * n);
Code language: C# (cs)
We use the lambda expression n => n*n
as the transform function that takes each element of the numbers array, squares it, and returns the resulting square numbers as the element of the new sequence.
Third, write each element of the new sequence to the console using a foreach
loop and Write()
method:
foreach (var square in squares)
{
Write($"{square} ");
}
Code language: C# (cs)
If you run the program, you’ll see the following output:
1 25 9 16 49
Code language: C# (cs)
Note that you can chain the Select()
method with other extensions method like OrderBy()
, Where()
, etc.
For example, the following program demonstrates how to chain the Select()
method with the OrderBy()
method to sort the new sequence:
using static System.Console;
var numbers = new[] { 1, 5, 3, 4, 7 };
var squares = numbers.Select(n => n * n)
.OrderBy(n => n);
foreach (var square in squares)
{
Write($"{square} ");
}
Code language: C# (cs)
Output:
1 9 16 25 49
Code language: C# (cs)
2) Using the LINQ Select() method to transform a sequence of strings
The following program demonstrates how to use Select()
method to capitalize each string in an array of greetings and write out the modified string to the console:
using static System.Console;
var greetings = new string[] { "hi","hello","hey", "howdy"};
var results = greetings.Select(
s => s[0].ToString().ToUpper() + s.Substring(1)
);
foreach (var result in results)
{
WriteLine(result);
}
Code language: C# (cs)
First, define an array of strings called greetings
that consists of four strings.
Second, create a new sequence of strings called results
using the Select()
method. The lambda expression s => s[0].ToString().ToUpper() + s.Substring(1)
takes each element of the greetings and capitalizes it.
Third, write each element in the results
sequence to the console:
foreach (var result in results)
{
WriteLine(result);
}
Code language: C# (cs)
3) Using the LINQ Select() method to project over a sequence of objects
The following program illustrates how to use the Select()
method to get the name of each employee:
using static System.Console;
namespace LINQDemo
{
class Employee
{
public string? Name { get; set; }
public string? Department { get; set; }
public int Salary { get; set; }
}
class Program
{
public static void Main()
{
var employees = new List<Employee>() {
new Employee { Name = "John", Department = "HR", Salary = 50000 },
new Employee { Name = "Jane", Department = "IT", Salary = 60000 },
new Employee { Name = "Bob", Department = "HR", Salary = 45000 },
new Employee { Name = "Sara", Department = "IT", Salary = 55000 },
new Employee { Name = "Tom", Department = "IT", Salary = 65000 }
};
var names = employees.Select(e => e.Name);
foreach (var name in names)
{
WriteLine(name);
}
}
}
}
Code language: C# (cs)
How it works.
First, define an Employee
class containing three properties Name
, Department
, and Salary
:
class Employee
{
public string? Name { get; set; }
public string? Department { get; set; }
public int Salary { get; set; }
}
Code language: C# (cs)
Second, define a list of employees:
var employees = new List<Employee>() {
new Employee { Name = "John", Department = "HR", Salary = 50000 },
new Employee { Name = "Jane", Department = "IT", Salary = 60000 },
new Employee { Name = "Bob", Department = "HR", Salary = 45000 },
new Employee { Name = "Sara", Department = "IT", Salary = 55000 },
new Employee { Name = "Tom", Department = "IT", Salary = 65000 }
};
Code language: C# (cs)
Third, transform a list of Employee
objects into a list of names using the Select()
method. The lambda expression e => e.Name
returns the full name of each Employee
object:
var names = employees.Select(e => e.Name);
Code language: C# (cs)
Finally, write each element of the names
sequence to the console:
foreach (var name in names)
{
WriteLine(name);
}
Code language: C# (cs)
Output:
John
Jane
Bob
Sara
Tom
Code language: C# (cs)
Also, you can use the Select()
method to create a sequence of elements with new a new type. For example:
using static System.Console;
namespace LINQDemo
{
class Employee
{
public string? Name { get; set; }
public string? Department { get; set; }
public int Salary { get; set; }
}
class Program
{
public static void Main()
{
var employees = new List<Employee>() {
new Employee { Name = "John", Department = "HR", Salary = 50000 },
new Employee { Name = "Jane", Department = "IT", Salary = 60000 },
new Employee { Name = "Bob", Department = "HR", Salary = 45000 },
new Employee { Name = "Sara", Department = "IT", Salary = 55000 },
new Employee { Name = "Tom", Department = "IT", Salary = 65000 }
};
var payroll = employees.Select(e => new {
e.Name,
e.Salary
});
foreach (var employee in payroll)
{
WriteLine(employee);
}
}
}
}
Code language: C# (cs)
Output:
{ Name = John, Salary = 50000 }
{ Name = Jane, Salary = 60000 }
{ Name = Bob, Salary = 45000 }
{ Name = Sara, Salary = 55000 }
{ Name = Tom, Salary = 65000 }
Code language: C# (cs)
In this example, the lambda expression returns an object with a new anonymous type with two properties Name
and Salary
:
e => new {
e.Name,
e.Salary
}
Code language: C# (cs)
Summary
- Use the LINQ
Select()
method to project each element of a sequence into a new form.