C# LINQ select Clause

Updated on 13 Sep 2012,
Published on 07 Sep 2010

In a LINQ query expression, the select clause specifies the type of the result sequence returned after the execution of query. By default the type of the result is based on the range variable specified in the first from clause. If there are multiple expressions before the final select clause in the query then the result type is based on the evaluation of all the specified expressions. The select clause of LINQ query expressions also provides a powerful mechanism that allows you to project or transform the results of any data source into new types.

Syntax for LINQ select Clause

from <range-variable> in <data source sequence object>

select <range-variable>

the above syntax shows that by default select clause accepts the name of the range-variable that determines the type of the query variable.

Example for LINQ select Clause using C# in ASP.Net

List<int> marksList = new List<int>() { 91, 75, 33, 45, 65, 68, 93 };

IEnumerable<string> gradeA_Marks = from marks in marksList
                                   where marks > 90
                                   select marks.ToString();
        
foreach (string m in gradeA_Marks)
{
    Response.Output.Write("{0} ", m);
}
        
// Output:
// 91 93

In the above C# LINQ select clause example we have used a simple data source of generic list collection of integer values. The LINQ query expression has from statement, where statement and finally the select statement. In the example code we have used the ToString() C# method to covert the type of the result sequence from int to string to store the string type results sequence into the IEnumerable<string> type query variable gradeA_marks. To use the default type you can use IEnumerable<int> type query variable and remove the ToString() method from the select clause.

Continue to the next tutorial: LINQ group Clause using C# in ASP.Net to learn how to group the result sequence based on the specified grouping key.

0 Responses to "C# LINQ select Clause"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers