You already know that LINQ is a powerful feature in C# that enables developers to query and manipulate data in a more expressive and concise manner. One of the essential components of LINQ is the Select projection operator or method. In this tutorial, we will explore the LINQ Select operator/method in C# and demonstrate its usage with various examples.

What is Projection in LINQ?

In LINQ, projection is nothing but a mechanism, which allows you to retrieve and manipulate only the necessary data or properties from a data source or collection and create a new sequence based on your requirements. By reducing the amount of data transferred or processed, it helps optimize performance, improve readability, and simplify data manipulation operations.

The projection operation can involve selecting individual properties, performing calculations, applying to format, creating anonymous types, or even joining multiple collections to create a composite result set. The result of a projection operation is often a new sequence or collection that contains the projected elements.

Understanding the Select Projection Operator/Method

LINQ provides the Select projection operator/method to perform projection on collections or data sources. The Select operator/method takes a lambda expression or delegates as a parameter, defining the projection logic for each element in the source collection. It allows you to specify which properties or values to include in the projected result set.

Basic Syntax

The basic syntax for the Select operator/method is as follows:

Here, collection represents the source collection, element represents an individual element in the collection, and transformation represents the logic to transform the element. The result is stored in an IEnumerable<TResult> or a compatible collection.

Understand Select Method and Select Operator

For better understanding, we have to follow the below steps:

  1. Create a Console Application.
  2. Now create the “Employee” class with the four properties such as ID, FirstName, LastName, and Salary.
  3. Also, create one static method which will return the hard-coded list of employees which will act as our data source.
  4. The point that you need to remember while using the Query Syntax.

Let us discuss some examples to understand the LINQ Select Operator or Select Method. 

Select Single Property by Select Operator/Method

Our requirement is to Select all the Employee IDs using both LINQ Method and Query syntax. In that case, within the Select Operator or Select Method, we need to specify the ID property.

In this scenario, the data type of the basicPropQuery variable is List<int>, this is because of the ToList() method that we applied to the Query Syntax. And because of this ToList() method, the query is executed at that point only.

But in the case of Method Syntax, we have not applied the ToList() method and this is the reason why the data type of the basicPropMethod variable is of IEnumerable<int> type. And more importantly, at that point, the query is just generated but not executed. When we use the basicPropMethod variable within the for-each loop, then at that time the query is going to be executed.

Select Multiple Properties by Select Operator/Method

Now, let us proceed and try to understand how to select multiple properties using LINQ Select Projection Operator and Method with an example. Our requirement is to select only the Employee’s First Name, Last Name, and Salary properties. We don’t require you to select the ID property of the Employee. The Complete Example Code is Given Below.

In this scenario, we are selecting the First Name, Last Name, and Salary properties of the same Employee class. Later, we will show you how to project these properties to a different class and to an anonymous type. With the Select Operator or Select Method, we are creating an instance of the Employee class and populating the First Name, Last Name, and Salary properties from the data source which we can access using the emp object.

Select Multiple Properties From Different Class using LINQ Projection Operator?

It is also possible to select the data to a different class using the LINQ Select Operator or Select Method. Let us create a new class with the above three properties and to this class, we will project the data. So, create a new class file with the name EmployeeBasicInfo.cs and then copy and paste the following code into it.

Now, we need to select the First Name, Last Name, and Salary properties to the above newly created EmployeeBasicInfo class. With the Select Method or select operator, we are not creating an instance of the Employee class, rather we are creating an instance of the EmployeeBasicInfo class and populating the FirstName, LastName, and Salary properties from the data source which we can access using the emp object.

Select Anonymous Type Data by Select Operator/Method

Instead of projecting the data source data to a particular type like Employee or EmployeeBasicInfo, we can also project the data to an anonymous type in LINQ using the Select Method or select operator. For a better understanding, please have a look at the given example code. 

In this scenario, we are creating an anonymous object (i.e. creating an object without specifying the type) and creating and populating the FirstName, LastName, and Salary properties from the data source which we can access using the emp object.

Perform Calculations on Select Data

We want to perform the following calculations on the selected employee data. That means we need to calculate the Annual Salary and we need to merge the First Name and Last Name as Full Name in the output.

  1. AnnualSalary = Salary*12
  2. FullName = FirstName + ” ” + LastName

Once we do the above calculation, then we need to project the ID, AnnualSalary, and FullName to an anonymous type using the LINQ Projection Operator. For a better understanding, please have a look at The Complete Example Code Below.

Select Data with Index Value using LINQ

It is also possible to select values using an integral index. The index is 0 based. If the query fetches five records, then the index will be from 0 to 4. It’s basically a unique value to each record that we select or project from the data source. The Complete Example Code is Given Below.

The LINQ Select projection operator/method is a powerful tool for transforming elements of a collection based on specific requirements. It allows you to select individual properties, perform custom transformations, and project elements into anonymous types. By mastering the Select operator/method, you can write cleaner and more expressive code when working with collections in C#. Experiment with different scenarios and explore the full potential of LINQ to enhance your productivity as a C# developer.

Back to Course Content

Leave a Comment