c sharp

What is C# LINQ Select

C# provides a variety of features to developers that manipulate data from databases or different collections. LINQ is also recognized as one of the main features that provide an integrated environment to alter the queries in which various operators are used. The select operator is used with the LINQ to project or transform the data sequence.

In this guide, we will explore the select LINQ feature in C#.

What is LINQ Select in C#?

In C#, the LINQ select method is used for transforming components in a collection of data or list objects. The select is an operator which is used to select or shape the properties of the display section according to the user’s requirements. It accepts an expression called “lambda” as input, transforms each element according to the transformation logic, and then produces a new series containing the modified elements.

Syntax

The select operator in LINQ can be represented by either query expression syntax or method syntax which are provided below:

Query Expression Syntax

In query expression syntax, the “select” operator is used to select the transformation. As given below:

var query = from item in source

  select expression;

Here, the “item” defines an element of the collection “source”, and the “expression” specifies the transformation logic that will be performed on each element.

Method Syntax

The “select” operator is used as a method on a source collection in method syntax as follows:

var query = source.Select(item => expression);

In the above syntax, “source” is the collection, and “item” defines each element that is represented by the parameter of the lambda expression “(item => expression)” that represents the logic of transformation.

Return Type: It returns “IEnumerable<TResult>”.

Example: LINQ Select with Query and Method in C#

Here is a demonstration of how to use the LINQ Select operator in C# while using both the query expression and the method:

using System;

using System.Linq;

using System.Collections.Generic;

Here:

  • using System;” gives the .NET framework’s essential classes and features.
  • using System.Linq;” offers simple query operators for querying and modifying sets of data.
  • using System.Collections.Generic;” is referred to as a namespace, providing generic-based collections of classes and interfaces for type-safe data storage and manipulation.

In the below-provided code, we declared and initialized the public “Program” class that has a public static “void Main()” method. Inside the “Main()” method, declared a list of “Days” as a sample collection of string-type list items:

public class Program

{

    public static void Main()

    {// Sample collection of Days

        List<string> Days = new List<string> { "Monday", "Tuesday", "Wednesday", "Friday", "Saturday", "Sunday" };

Then, use the query expression. For instance, the “Days” from the collection of item “name” in the “query_ExpressionResult” variable. After this, the “select” operator transforms each “name” to uppercase with the “ToUpper()” function in both query expression and method from LINQ Select. Next, the “method_SyntaxResult” variable is used to save the “Days” using “Select()” methods having a parameter as lambda expression “name => name.ToUpper()“ to transform name to uppercase:

var query_ExpressionResult = from name in Days

    select name.ToUpper();

var method_SyntaxResult = Days.Select(name => name.ToUpper());

Note: Both variables, such as the “query_ExpressionResult”, and “method_SyntaxResult” save the transformed result of “Days” from the “name” item with query expression and method, respectively.

Finally, the “foreach” loop is used to iterate the “name” from both “query_ExpressionResult”, and “method_SyntaxResult”. Lastly, display the resultant list by using the “Console.WriteLine()” statement one by one:

        Console.WriteLine("****************************");
        Console.WriteLine("Display names of list using query expression:");
        Console.WriteLine("****************************");
        foreach (var name in query_ExpressionResult)
        {
            Console.WriteLine(name);
        }
       
        Console.WriteLine("\n");
        Console.WriteLine("****************************");
        Console.WriteLine("Display names of list using method syntax");
        Console.WriteLine("****************************");
        foreach (var name in method_SyntaxResult)
        {
            Console.WriteLine(name);
        }
    }
}

The output of the above-provided code has been displayed in the below-given image:

That’s all! We have described the detailed information about the LINQ select method in C#.

Conclusion

The LINQ’s “select” method is an efficient approach for transforming the collection of elements in keeping with a specified logic. It makes the procedure of manipulating data simpler. In this guide, we have demonstrated the LINQ select method in C#.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.