15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

294 ❘ ChaPTer 11 lAnGuAGe inteGrAted Query<br />

The Where() method of LINQ to objects is defined with the Enumerable class:<br />

public static IEnumerable Where(this IEnumerable source,<br />

Func predicate);<br />

Inside the System.Linq namespace there’s another class that implements the operator Where. This<br />

implementation is used by LINQ to SQL. You can find the implementation in the class Queryable:<br />

public static IQueryable Where(this IQueryable source,<br />

Expression predicate);<br />

Both of these classes are implemented in the System.Core assembly in the System.Linq namespace. How<br />

is it defined <strong>and</strong> what method is used The Lambda expression is the same no matter whether it is passed<br />

with a Func parameter or with an Expression parameter.<br />

Just the compiler behaves differently. The selection is done based on the source parameter. The method<br />

that matches best based on its parameters is chosen by the compiler. The CreateQuery() method of the<br />

ObjectContext class that is defined by ADO.<strong>NET</strong> Entity Framework returns an ObjectQuery object<br />

that implements IQueryable, <strong>and</strong> thus the Entity Framework uses the Where() method of the<br />

Queryable class.<br />

summary<br />

In this chapter, you’ve seen the LINQ query <strong>and</strong> the language constructs that the query is based on, such<br />

as extension methods <strong>and</strong> Lambda expressions. You’ve seen the various LINQ query operators not just<br />

for filtering <strong>and</strong> ordering of data sources, but also for partitioning, grouping, doing conversions, joins,<br />

<strong>and</strong> so on.<br />

With Parallel LINQ you’ve seen how longer queries can easily be parallelized.<br />

Another important concept is the expression tree. Expression trees allow building the query to the data<br />

source at runtime because the tree is stored in the assembly. You can read about the great advantages of it<br />

in Chapter 31. LINQ is a very in-depth topic, <strong>and</strong> you should see Chapters 31 <strong>and</strong> 33 for more information.<br />

Other third-party providers are available for download; for example, LINQ to MySQL, LINQ to<br />

Amazon, LINQ to Flickr, LINQ to LDAP, <strong>and</strong> LINQ to SharePoint. No matter what data source you<br />

have, with LINQ you can use the same query syntax.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!