C# Keywords Tutorial Part 74: select

C# Keywords Tutorial Part 74: select

C# is a highly adaptable programming language that empowers developers to design potent and effective applications. Among the various functionalities of C#, the “select” keyword is a widely used feature in LINQ (Language-Integrated Query) statements that enable the filtration and modification of data. The primary focus of this blog post is to delve into the “select” keyword in C# and showcase its practical usage through relevant code examples.

What is the “select” keyword in C#?

In LINQ queries, the “select” keyword is used to change the shape of a query’s results. It comes before the “where” and “orderby” keywords and is used following the “from” keyword. The “select” keyword accepts an expression that controls the transformation of the query results. The expression might be as straightforward as a projection or as sophisticated as an expression that transforms the data using operators.

Code examples of using “select” in C#

Here are some code examples to illustrate how the “select” keyword can be used in C#.

Example 1: Simple Projection

In this example, we have a list of integers and we want to create a new list that contains the square of each integer.

List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var squares = from n in numbers
              select n * n;

In this code, the “select” keyword takes the expression “n * n” which transforms each number in the “numbers” list into its square. The resulting “squares” variable contains a new list with the squared values.

Example 2: Projection with Anonymous Types

In this example, we have a list of employees and we want to create a new list that contains the employee’s name and their salary.

class Employee {
    public string Name { get; set; }
    public int Salary { get; set; }
}

List<Employee> employees = new List<Employee> {
    new Employee { Name = "John", Salary = 50000 },
    new Employee { Name = "Jane", Salary = 60000 },
    new Employee { Name = "Bob", Salary = 70000 }
};

var employeeInfo = from e in employees
                   select new {
                       e.Name,
                       e.Salary
                   };

In this code, the “select” keyword takes an expression that creates a new anonymous type with the employee’s name and salary. The resulting “employeeInfo” variable contains a new list with the name and salary of each employee.

Example 3: Projection with Operators

In this example, we have a list of strings and we want to create a new list that contains the length of each string.

List<string> names = new List<string> { "John", "Jane", "Bob" };
var nameLengths = from n in names
                  select n.Length;

In this code, the “select” keyword takes an expression that uses the “Length” property of each string to get the length of the string. The resulting “nameLengths” variable contains a new list with the length of each string.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *