الاثنين، 11 أبريل 2011

Simple Intro to Linq

Linq
LINQ is a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.
We use the term language-integrated query to indicate that query is an integrated feature of the developer's primary programming languages (for example, Visual C#, Visual Basic). Language-integrated query allows query expressions to benefit from the rich metadata, compile-time syntax checking, static typing and IntelliSense that was previously available only to imperative code. Language-integrated query also allows a single general purpose declarative query facility to be applied to all in-memory information, not just information from external sources.
ADO.NET 3.5 introduces many new language and data access features to ADO.NET 2.0 and its DataSet oriented programming model. The primary objectives of these additions are to:
§  Increase programmer productivity by reducing the amount of code required to complete a particular data related task, such as querying and updating relational data sources, DataSets, or XML documents  
§  Enable strong type checking of query syntax for relational data and query result sets   
§  Reduce or eliminate what ’ s often called the impedance mismatch  between the code for managing data, which usually involves Structured Query Language (SQL), and object oriented (OO) programming.
§  Provide data - intensive Windows form projects with a local, synchronizable data cache derived from SQL Server Compact Edition v3.5 Enhance DataSet integration with n - tier  architecture
Microsoft includes five LINQ implementations in VS 2008 SP1:
LINQ to Objects, the default implementation, for querying in  memory  objects 
LINQ to SQL for querying SQL Server [Express], which includes a domain specific graphic designer for its object/relational mapping (O/RM) tool 
LINQ to DataSet for querying typed and untyped ADO.NET DataSets and standalone
DataTables 
LINQ to XML for querying and creating XML Infosets and documents 
LINQ to Entities for querying the Entity Framework’s EntitySets defined by the Entity Data
Model




Three Parts of a 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
using System;
using System.Linq;

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 n in numQuery)
        {
            Console.Write("{0,1} ", n);
        }
    }
}


1 التعليقات:

mohamed nossair يقول...

شكرا لى اهتمامك بتعليمنا ويسعدنا انا نشارك معك ونرد الجميل لك