13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Translations us<strong>in</strong>g IQueryable and IQueryProvider327Figure 12.2Class diagram based on the <strong>in</strong>terfaces <strong>in</strong>volved <strong>in</strong> IQueryablewords, call<strong>in</strong>g the GetEnumerator method) or by a call to the Execute method on anIQueryProvider, pass<strong>in</strong>g <strong>in</strong> an expression tree.So, with at least some grasp of what IQueryable is for, what is IQueryProvider?Well, we can do more than execute a query—we can also build a bigger query from it,which is the purpose of the standard query operators <strong>in</strong> LINQ. 2 To build up a query,we need to use the CreateQuery method on the relevant IQueryProvider. 3Th<strong>in</strong>k of a data source as a simple query (SELECT * FROM SomeTable <strong>in</strong> SQL, for<strong>in</strong>stance)—call<strong>in</strong>g Where, Select, OrderBy, and similar methods results <strong>in</strong> a differentquery, based on the first one. Given any IQueryable query, you can create a new queryby perform<strong>in</strong>g the follow<strong>in</strong>g steps:1 Ask the exist<strong>in</strong>g query for its query expression tree (us<strong>in</strong>g the Expression property).2 Build a new expression tree that conta<strong>in</strong>s the orig<strong>in</strong>al expression and the extrafunctionality you want (a filter, projection, or order<strong>in</strong>g, for <strong>in</strong>stance).2Well, the ones that keep deferr<strong>in</strong>g execution, such as Where and Jo<strong>in</strong>. We’ll see what happens with the aggregationssuch as Count <strong>in</strong> a little while.3Both Execute and CreateQuery have generic and nongeneric overloads. The nongeneric versions make iteasier to create queries dynamically <strong>in</strong> code. Compile-time query expressions use the generic version.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!