13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

Create successful ePaper yourself

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

Translations us<strong>in</strong>g IQueryable and IQueryProvider331from user <strong>in</strong> userswhere user.Name.StartsWith("D")select user.NameQuery expression translationusers.Where(user => user.Name.StartsWith("D").Select(user => user.Name)Overload resolutionIQueryable not implementedIQueryable implementedExtension methods on Enumerableare chosen, which use delegatesas parametersExtension methods on Queryableare chosen, which use expressiontrees as parametersIL to create delegate <strong>in</strong>stances, withcalls to Enumerable.Where andEnumerable.SelectIL to create expression trees, withcalls to Queryable.Where andQueryable.SelectFigure 12.3 A query tak<strong>in</strong>g two paths, depend<strong>in</strong>g on whether the data source implementsIQueryable or only IEnumerablepo<strong>in</strong>t, the lambda expressions can be converted to either delegate <strong>in</strong>stances or expressiontrees, potentially giv<strong>in</strong>g radically different implementations: typically <strong>in</strong>-memoryfor the left path, and SQL execut<strong>in</strong>g aga<strong>in</strong>st a database <strong>in</strong> the right path.The second big difference between Enumerable and Queryable is that theEnumerable extension methods do the actual work associated with the correspond<strong>in</strong>gquery operator. There is code <strong>in</strong> Enumerable.Where to execute the specified filter andonly yield appropriate elements as the result sequence, for example. By contrast, thequery operator “implementations” <strong>in</strong> Queryable do very little: they just create a newquery based on the parameters or call Execute on the query provider, as described atthe end of section 12.2.1. In other words, they are only used to build up queries andrequest that they be executed—they don’t conta<strong>in</strong> the logic beh<strong>in</strong>d the operators. Thismeans they’re suitable for any LINQ provider that uses expression trees.With the Queryable extension methods available and mak<strong>in</strong>g use of our IQueryableand IQueryProvider implementations, it’s f<strong>in</strong>ally time to see what happens when we usea query expression with our custom provider.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!