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.

Introduc<strong>in</strong>g LINQ279Figure 11.1 shows this query expressiongraphically, break<strong>in</strong>g it down <strong>in</strong>toits <strong>in</strong>dividual steps. I’ve <strong>in</strong>cluded anumber of similar figures <strong>in</strong> this chapter,but unfortunately for complicatedqueries there is simply not enoughroom on the pr<strong>in</strong>ted page to show asmuch data as we might like. Moredetailed diagrams are available on thebook’s website.Each arrow represents a sequence—the description is on the left side, andsome sample data is on the right. Eachbox is a transformation from our queryexpression. Initially, we have the wholefamily (as Person objects); then afterfilter<strong>in</strong>g, the sequence only conta<strong>in</strong>sadults (aga<strong>in</strong>, as Person objects); andthe f<strong>in</strong>al result has the names of thoseadults as str<strong>in</strong>gs. Each step simply takesone sequence and applies an operationAll "Person" objects<strong>in</strong> "people"All "Person" objects withan age of at least 18Names of people withan age of at least 18from person <strong>in</strong> peoplewhere person.Age >= 18select person.Name(Result of query)Name="Holly", Age=31Name="Tom", Age=4Name="Jon", Age=31Name="William", Age=1Name="Rob<strong>in</strong>", Age=1Name="Holly", Age=31Name="Jon", Age=31"Holly""Jon"Figure 11.1 A simple query expression broken down<strong>in</strong>to the sequences and transformations <strong>in</strong>volvedto produce a new sequence. The result isn’t the str<strong>in</strong>gs “Holly” and “Jon” —<strong>in</strong>stead, it’san IEnumerable, which, when asked for its elements one by one, will first yield“Holly” and then “Jon.”This example was straightforward to start with, but we’ll apply the same techniquelater to more complicated query expressions <strong>in</strong> order to understand them more easily.Some advanced operations <strong>in</strong>volve more than one sequence as <strong>in</strong>put, but it’s still a lotless to worry about than try<strong>in</strong>g to understand the whole query <strong>in</strong> one go.So, why are sequences so important? They’re the basis for a stream<strong>in</strong>g model fordata handl<strong>in</strong>g—one that allows us to process data only when we need to.DEFERRED EXECUTION AND STREAMINGWhen the query expression shown <strong>in</strong> figure 11.1 is created, no data is processed. Theorig<strong>in</strong>al list of people isn’t accessed at all. Instead, a representation of the query is builtup <strong>in</strong> memory. Delegate <strong>in</strong>stances are used to represent the predicate test<strong>in</strong>g for adulthoodand the conversion from a person to that person’s name. It’s only when the result<strong>in</strong>gIEnumerable is asked for its first element that the wheels start turn<strong>in</strong>g.This aspect of LINQ is called deferred execution. When the first element of the resultis requested, the Select transformation asks the Where transformation for its first element.The Where transformation asks the list for its first element, checks whether thepredicate matches (which it does <strong>in</strong> this case), and returns that element back toSelect. That <strong>in</strong> turn extracts the name and returns it as the result.That’s all a bit of a mouthful, but a sequence diagram makes it all much clearer.I’m go<strong>in</strong>g to collapse the calls to MoveNext and Current to a s<strong>in</strong>gle fetch operation: itLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!