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.

280 CHAPTER 11 Query expressions and LINQ to Objectsmakes the diagram a lot simpler. Just remember that each time the fetch occurs, it’seffectively check<strong>in</strong>g for the end of the sequence as well. Figure 11.2 shows the first fewstages of our sample query expression <strong>in</strong> operation, when we pr<strong>in</strong>t out each elementof the result us<strong>in</strong>g a foreach loop.As you can see <strong>in</strong> figure 11.2, only one element of data is processed at a time. If wedecided to stop pr<strong>in</strong>t<strong>in</strong>g output after writ<strong>in</strong>g “Holly,” we would never execute any ofthe operations on the other elements of the orig<strong>in</strong>al sequence. Although severalstages are <strong>in</strong>volved here, process<strong>in</strong>g data <strong>in</strong> a stream<strong>in</strong>g manner like this is efficient andCaller(foreach)Select Where Listfetchfetchfetchreturn {"Holly", 31}return {"Holly", 31}Check: Age >= 18? Yesreturn "Holly"Transform:{"Holly", 31} =>"Holly”Pr<strong>in</strong>t "Holly"fetchfetchfetchreturn {"Tom", 4}Check: Age >= 18? Nofetchreturn {"Jon", 31}return {"Jon", 31}Check: Age >= 18? Yesreturn "Jon"Transform:{"Jon", 31} =>"Jon"Pr<strong>in</strong>t "Jon"(and so on)Figure 11.2Sequence diagram of the execution of a query expressionLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!