Jul 3, 2008

LINQ - Introduction

LINQ (Language Integrated Query) introduces a generic and standard pattern for querying and updating data store of any kind. Basically a query is an expression that retrieves data from a data source. Queries are usually expressed in a specialized query language. I assume LINQ is intoduced to decouple the query language from the datasource. This avoids a lot of "unnecessary" learnings, because literally there is query language for each type of data source be it SQL databases, ADO.NET datasets, Collections types and XML documents.
A LINQ query operations has there distinct phases:

  Recognizing the Data Source
With LINQ, there is another concept that has been introduced called Queryable, implementing interface IQueryable. Any Enumerable type is a queryable type and it requires no modification as a LINQ data source. If the source data is not already in memory as a queryable type, the LINQ provider must represent it as such. For example, LINQ to XML loads an XML document into a queryable XElement type.Types such as ArrayList that support the non-generic IEnumerable interface can also be used as a LINQ data source.
Here we will take an example of array int.

int[] array = new int[] {0,1,2,3,4};

  Defining the Query
A query specifies what data to be retrieved from a data source and how that information should be sorted and grouped. A query is stored in a query variable and initialized with a query expression. To make it easier to write queries, C# has introduced new query syntax.

// myQuery is an IEnumerable of type int
var myQuery =
from n in array
where (n % 2) == 0
select n;

Here the query expression contains three clauses: from, where and select just like any SQL query. The "from" clause specifies the data source, the "where" clause applies the filter, and the "select" clause specifies the type of the returned elements.

The important point is that in LINQ, the query variable itself takes no action and returns no data. It just stores the information that is required to produce the results when the query is executed at some later point.

Queries can also be expressed by using method syntax.( Something I am not aware right now )

  Query Execution
The query variable itself only stores the query commands. The actual execution of the query is deferred until we iterate over the query variable in a foreach statement. This concept is regarded as "deferred execution" and is demonstrated in the following example:

foreach (int num in myQuery)
  Console.WriteLine( "{0}", num );

Because the query variable itself never holds the query results, we can execute it as often as we like. For example, we may have a database that is being updated continually by a separate application, we could create one query that retrieves the latest data, and we could execute it repeatedly at some interval to retrieve different results every time.
Whenever the query variable is accessed or any of it's method is called, the query gets executed and the result sets are cached in the respective objects. This term is regarded as "Forced execution".
You can refer msdn for further details.

No comments:

Post a Comment