The Select
operator is a LINQ query operator that projects each element of a sequence into a new form. The projection is specified by a function that takes an element of the sequence as an input and returns a new element or a new object that contains some or all of the properties of the input element.
The Select
operator is typically used to transform the elements of a sequence into a new form or to extract a subset of the properties of the elements. For example, you can use the Select
operator to extract the names of the people in a list of Person
objects, or to create a new sequence of the squares of a list of integers.
You can also use Select
operator to create a new object from the properties of an existing object, for example creating a new anonymous type from the selected properties of a class.
It’s worth noting that the Select
operator returns an IEnumerable<T>
object, which is a lazy-evaluated sequence, so the query is not executed until the results are enumerated or materialized.
The Select
operator is a very powerful tool that can be used in many different ways to transform and shape data in a variety of ways, it’s a fundamental operator that is used extensively in LINQ.
LINQ Select Examples
Example 1: Projecting a list of integers to a list of their squares:
1 2 3 4 | List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var squares = numbers.Select(n => n * n); |
Example 2: Selecting properties of a list of objects:
1 2 3 4 | List<Person> people = GetPeople(); var names = people.Select(p => p.Name); |
Example 3: Selecting a subset of properties from a list of objects:
1 2 3 4 | List<Order> orders = GetOrders(); var orderDetails = orders.Select(o => new { o.Id, o.Customer.Name, o.TotalAmount }); |
Example 4: Selecting and flattening a nested collection:
1 2 3 4 | List<Department> departments = GetDepartments(); var employeeNames = departments.SelectMany(d => d.Employees.Select(e => e.Name)); |
Example 5: Selecting and transforming a list of objects into a dictionary:
1 2 3 4 | List<Product> products = GetProducts(); var productDict = products.ToDictionary(p => p.Id, p => p.Name); |
Example 6: Projecting a list of integers to a list of their squares and cubes:
1 2 3 4 | List<int> numbers = new List<int> { 1, 2, 3, 4, 5 }; var results = numbers.Select(n => new { Square = n * n, Cube = n * n * n }); |
Example 7: Selecting a specific property from a list of objects and creating a new list of that property, then transforming it into a new form:
1 2 3 4 | List<Product> products = GetProducts(); var discountedPrices = products.Select(p => p.Price * 0.9); |
Example 8: Selecting a specific property from a list of objects, creating a new list of that property, then grouping it by another property:
1 2 3 4 | List<Order> orders = GetOrders(); var ordersByType = orders.Select(o => new { o.ProductType, o.TotalAmount }).GroupBy(o => o.ProductType); |
Example 9: Selecting a specific property from a list of objects, creating a new list of that property and then sorting it:
1 2 3 4 | List<Employee> employees = GetEmployees(); var sortedSalaries = employees.Select(e => e.Salary).OrderByDescending(s => s); |
Example 10: Selecting a specific property from a list of objects, creating a new list of that property, then using it as a key in a dictionary:
1 2 3 4 | List<Book> books = GetBooks(); var booksByTitle = books.ToDictionary(b => b.Title, b => b); |
[…] Select: Projects each element of a sequence into a new form […]