LINQ – Langage Integrated Query

List<strings> (list of strings)
A programming language mechanism by which a single piece of code (function, object, interface, etc.) can manipulate many different data types without explicitly having a separate entity for each data type
 Here is why the select clause appears on the last line of LINQ query:
Query expressions in LINQ are fully type-checked ans intellisense aware.
If the select clause came first , the IDE and the complier would not immediately know the type of data you wanted to query.
Northwind db = new Northwind(@"C:\Data\Northwnd.mdf");

var query = from c in db.Customers
            select c;

instead of
SqlConnection sqlConnection = new SqlConnection(connectString);
System.Data.SqlClient.SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = "Select * from Customer";
return sqlCommand.ExecuteReader(CommandBehavior.CloseConnection
This point was illustrated in the preceding chapter, where you saw three very similar queries that drew data from three different data sources: objects, an SQL database, and XML
var query = from c in GetCustomers()
            where c.City == "Mexico D.F."
            select new { City = c.City, ContactName = c.ContactName };

var query = from c in db.Customers
            where c.City == "Mexico D.F."
            select new { City = c.City, ContactName = c.ContactName };

var query = from x in customers.Descendants("Customer")
            where x.Attribute("City").Value == "Mexico D.F."
            select x;


Using the LINQ provider model, developers can extend LINQ to query other data sources besides . The following are a few of the data sources currently enabled by third-party LINQ providers:

LINQ Extender
LINQ to Google
LINQ over C# project
LINQ to Indexes
LINQ to Active Directory
LINQ to IQueryable
LINQ to Amazon
LINQ to JavaScript
LINQ to Bindable Sources
LINQ to Excel
LINQ to Expressions
LINQ to Lucene
LINQ to Flickr
LINQ to Metaweb
LINQ to Geo
LINQ to NCover
LINQ to Sharepoint
LINQ to NHibernate
LINQ to SimpleDB
LINQ to Opf3
LINQ to Streams
LINQ to Parallel (PLINQ)
LINQ to WebQueries
LINQ to RDF Files

These projects are of varying quality.



var query1 = from place in terraPlaces
             where place.Name == "Portland"
             select new { place.Name, place.State };

This query returns a number of locations, but here are a few of the more prominent:

{ Name = Portland, State = Indiana }
{ Name = Portland, State = Maine }
{ Name = Portland, State = Michigan }
{ Name = Portland, State = Oregon }
{ Name = Portland, State = Texas }
{ Name = Portland, State = Alabama }
{ Name = Portland, State = Arkansas }
{ Name = Portland, State = Colorado }
List<int> list01 = new List<int> { 1, 2, 3 };
List<int> list02 = new List<int> { 4, 5, 6 };
List<int> list03 = new List<int> { 7, 8, 9 };

List<List<int>> lists = new List<List<int>> { list01, list02, list03 };

Here is imperative code for accessing the members of this list:

List<int> newList = new List<int>();

foreach (var item in lists)
    foreach (var number in item)

Contrast this code with the declarative style used in a LINQ program:

var newList = from list in lists
              from num in list
              select num;

LINQ, on the other hand, can return a set of Customer objects, each of which owns a set of 0-to-n Orders. Each Order can be associated with a set of OrderDetails. This is a classic hierarchical relationship that can be perfectly expressed with a set of objects:




Consider the following simple hierarchical query that captures the relationship between two objects:

var query = from c in db.Customers
            select new { City = c.City,
                         orders = from o in c.Orders
                         select new { o.OrderID }


LINQ queries are composable: You can combine them in multiple ways, and one query can be used as the building block for yet another query. To see how this works, let’s look at a simple query:

var query = from customer in db.Customers
            where customer.City == "Paris"
            select customer;

The variable that is returned from the query is sometimes called a computation. If you write a foreach loop and display the address field from the customers returned by this computation, you see the following output:

265, boulevard Charonne
25, rue Lauriston

You can now write a second query against the results of this query:

query2 = from customer in query
        where customer.Address.StartsWith("25")
        select customer;

Notice that the last word in the first line of this query is the computation returned from the previous query. This second query produces the following output:

25, rue Lauriston

LINQ to Objects queries are composable because they operate on and usually return variables of type IEnumerable<T>. In other words, LINQ queries typically follow this pattern:

IEnumerable<T> query = from x in IEnumerable<T>
                       select x;



Simple Query That Transforms the Results of a LINQ to SQL Query into XML

var query = new XElement("Orders", from c in db.Customers
              where c.City == "Paris"
              select new XElement("Order",
                 new XAttribute("Address", c.Address),
                 new XAttribute("City", c.City)));
Cet article a été publié dans DotNet - LINQ. Ajoutez ce permalien à vos favoris.

Laisser un commentaire

Entrez vos coordonnées ci-dessous ou cliquez sur une icône pour vous connecter:


Vous commentez à l'aide de votre compte Déconnexion /  Changer )

Photo Google+

Vous commentez à l'aide de votre compte Google+. Déconnexion /  Changer )

Image Twitter

Vous commentez à l'aide de votre compte Twitter. Déconnexion /  Changer )

Photo Facebook

Vous commentez à l'aide de votre compte Facebook. Déconnexion /  Changer )


Connexion à %s