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.

286 CHAPTER 11 Query expressions and LINQ to Objectsstatic void Ma<strong>in</strong>(){var source = new Dummy();Creates sourceto be queried}}var query = from dummy <strong>in</strong> sourcewhere dummy.ToStr<strong>in</strong>g()=="Ignored"select "Anyth<strong>in</strong>g";Runn<strong>in</strong>g list<strong>in</strong>g 11.3 pr<strong>in</strong>ts “Where called” and then “Select called” just as we’dexpect, because the query expression has been translated <strong>in</strong>to this code:var query = source.Where(dummy => dummy.ToStr<strong>in</strong>g()=="Ignored").Select(dummy => "Anyth<strong>in</strong>g");Calls methods via aquery expressionOf course, we’re not do<strong>in</strong>g any query<strong>in</strong>g or transformation here, but it shows how thecompiler is translat<strong>in</strong>g our query expression. If you’re puzzled as to why we’ve selected"Anyth<strong>in</strong>g" <strong>in</strong>stead of just dummy, it’s because a projection of just dummy (which is a “donoth<strong>in</strong>g” projection) would be removed by the compiler <strong>in</strong> this particular case. We’lllook at that later <strong>in</strong> section 11.3.2, but for the moment the important idea is the overalltype of translation <strong>in</strong>volved. We only need to learn what translations the <strong>C#</strong> compilerwill use, and then we can take any query expression, convert it <strong>in</strong>to the form thatdoesn’t use query expressions, and then look at what it’s do<strong>in</strong>g from that po<strong>in</strong>t of view.Notice how we don’t implement IEnumerable at all <strong>in</strong> Dummy. The translationfrom query expressions to “normal” code doesn’t depend on it, but <strong>in</strong> practice almostall LINQ providers will expose data either as IEnumerable or IQueryable (whichwe’ll look at <strong>in</strong> chapter 12). The fact that the translation doesn’t depend on any particulartypes but merely on the method names and parameters is a sort of compile-timeform of duck typ<strong>in</strong>g. This is similar to the same way that the collection <strong>in</strong>itializers presented<strong>in</strong> chapter 8 f<strong>in</strong>d a public method called Add us<strong>in</strong>g normal overload resolutionrather than us<strong>in</strong>g an <strong>in</strong>terface conta<strong>in</strong><strong>in</strong>g an Add method with a particular signature.Query expressions take this idea one step further—the translation occurs early <strong>in</strong> thecompilation process <strong>in</strong> order to allow the compiler to pick either <strong>in</strong>stance methods orextension methods. You could even consider the translation to be the work of a separatepreprocess<strong>in</strong>g eng<strong>in</strong>e.NOTE Why from … where … select <strong>in</strong>stead of select … from … where? Manydevelopers f<strong>in</strong>d the order of the clauses <strong>in</strong> query expressions confus<strong>in</strong>gto start with. It looks just like SQL—except back to front. If you look backto the translation <strong>in</strong>to methods, you’ll see the ma<strong>in</strong> reason beh<strong>in</strong>d it. Thequery expression is processed <strong>in</strong> the same order that it’s written: we startwith a source <strong>in</strong> the from clause, then filter it <strong>in</strong> the where clause, thenproject it <strong>in</strong> the select clause. Another way of look<strong>in</strong>g at it is to considerthe diagrams throughout this chapter. The data flows from top to bottom,and the boxes appear <strong>in</strong> the diagram <strong>in</strong> the same order as their correspond<strong>in</strong>gclauses appear <strong>in</strong> the query expression. Once you get overany <strong>in</strong>itial discomfort due to unfamiliarity, you may well f<strong>in</strong>d thisapproach appeal<strong>in</strong>g—I certa<strong>in</strong>ly do.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!