Monday, January 18, 2016

Linq Introduction

LINQ Introduction

Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.

Three Parts of Query operation

All LINQ query operations consist of three distinct actions:
1.       Obtain the data source.
2.       Create the query.
3.       Execute the query.
The following example shows how the three parts of a query operation are expressed in source code. The example uses an integer array as a data source for convenience; however, the same concepts apply to other data sources also.

Example

class LINQExample
{       
    static void Main()
    {
        // The Three Parts of a LINQ Query:
        //  1. Data source.
        int[] numbs = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

        // 2. Query creation.
        // numQuery is an IEnumerable<int>
        var numQry =
            from num in numbs
            where (num % 2) == 0
            select num;

        // 3. Query execution.
        foreach (int num in numQry)
        {
            Console.Write("{0,1} ", num);
        }
    }
}


Data Source

In above example the data source is an inter array. Linq To SQL and LINQ to XML are also available
to fetch data from different sources

Types of LINQ
The types of LINQ are mentioned below in brief.
·         LINQ to Objects
·         LINQ to XML(XLINQ)
·         LINQ to DataSet
·         LINQ to SQL (DLINQ)
·         LINQ to Entities
Query

The query specifies what information to retrieve from the data source or sources. Optionally, a query also specifies how that information should be sorted, grouped, and shaped before it is returned. A query is stored in a query variable and initialized with a query expression. 

In above example it retrieve all even numbers from integer array. The query expression contains three clauses: from, where and select. The from specify the data source, where specify the
filters and select specify the type of returned elements


Query Excution

Deferred Execution

The query variable itself only stores the query commands. The actual execution of the query is deferred until you iterate over the query variable in a foreach statement. This concept is referred to as deferred execution and is demonstrated in the following example:


foreach (int num in numQuery)
{
    Console.Write("{0,1} ", num);
}

The foreach statement is also where the query results are retrieved. For example, in the previous query, the iteration variable num holds each value (one at a time) in the returned sequence.


Forcing Immediate Execution

Queries that perform aggregation functions over a range of source elements must first iterate over those elements. Examples of such queries are CountMaxAverage, and First. These execute without an explicit foreach statement because the query itself must useforeach in order to return a result. Note also that these tpes of queries return a single value, not an IEnumerable collection. The following query returns a count of the even numbers in the source array:

var evenNumQuery = from num in numbers
    where (num % 2) == 0
    select num;

To force immediate execution of any
query and cache its results, you can call the ToList<TSource> or ToArray<TSource> methods.


Syntax of LINQ

There are two syntaxes of LINQ. These are the following ones.
·         Lamda (Method) Syntax 
·         Example
var longWords = words.Where( w => w.length > 10);
·         Query (Comprehension) Syntax
·         Example

var longwords = from w in words where w.length > 10;

No comments:

Post a Comment