C# LINQ from Clause

Updated on 13 Sep 2012,
Published on 07 Sep 2010

A LINQ query expression always begin with a from clause. Moreover, a query can also have sub-query expressions that may also begin with from clause. It has following specification the LINQ query expression:

1. It specifies the data source sequence in the query for which the query is to be executed.

2. The from clause contains a range variable that represents each element in the data source sequence specified in the expression.

Syntax for LINQ from clause

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

In the above syntax you can see that the from clause contains a range variable and a data source sequence. Both of them are strongly typed identifiers. The data source specified in the from clause must have a type of IEnumerable, IEnumerable<T> or IQueryable<T>. The data source must be of .Net collection type that supports the LINQ extension methods.

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

int[] numbersArray = { 37, 40, 20, 23, 25, 27, 30, 5, 7, 10, 13, 33, 35, 15, 17 };

var numbers = from n in numbersArray
              select n;

foreach (int number in numbers)
{
    Response.Output.Write("{0} ", number);
}

// Output:
// 37 40 20 23 25 27 30 5 7 10 13 33 35 15 17

The above C# LINQ example illustrates the use of from clause in the query expression. numbersArray is an int type array that is a simple data source sequence which has been used in the query expression example. The variable n used in the query is a range variable that references each integer type element stored in the specified data source i.e. numbersArray.

Continue to next tutorial: LINQ where Clause using C# in ASP.Net to learn how to apply the filter on results sequence of from clause.

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