LINQ

The big reason why one has to learn LINQ, is that it provides the flexibility to query any datasource. LINQ can be used to query, in principle, any data source whatsoever like an SQL or XML.

Before LINQ  a developer has to know SQL to query relational database or XQuery to query. LINQ simplifies this situation by offering a consistent model for working with data across various kinds of data sources and formats.

LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, .NET collections, and any other format for which a LINQ provider is available.


All LINQ query operations consist of three distinct actions:


1.Obtain the data source.

2.Create the query.

3.Execute the query.

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

        // 2. Query creation.
        // numQuery is an IEnumerable
        var numQuery =
            from num in numbers
            where (num % 2) == 0
            select num;

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

 

* A queryable type requires no modification or special treatment to serve 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

 

 

 

Search site

© 2010 All rights reserved.