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.

Introduc<strong>in</strong>g LINQ281flexible. In particular, regardless of how much source data there is, you don’t need toknow about more than one element of it at any one po<strong>in</strong>t <strong>in</strong> time. That’s the differencebetween us<strong>in</strong>g List.ConvertAll and Enumerable.Select: the former creates awhole new <strong>in</strong>-memory list, whereas the latter just iterates through the orig<strong>in</strong>alsequence, yield<strong>in</strong>g a s<strong>in</strong>gle converted element at a time.This is a best-case scenario, however. There are times where <strong>in</strong> order to fetch thefirst result of a query, you have to evaluate all of the data from the source. We’vealready seen one example of this <strong>in</strong> the previous chapter: the Enumerable.Reversemethod needs to fetch all the data available <strong>in</strong> order to return the last orig<strong>in</strong>al elementas the first element of the result<strong>in</strong>g sequence. This makes Reverse a buffer<strong>in</strong>goperation—which can have a huge effect on the efficiency (or even feasibility) of youroverall operation. If you can’t afford to have all the data <strong>in</strong> memory at one time, youcan’t use buffer<strong>in</strong>g operations.Just as the stream<strong>in</strong>g aspect depends on which operation you perform, some transformationstake place as soon as you call them, rather than us<strong>in</strong>g deferred execution.This is called immediate execution. Generally speak<strong>in</strong>g, operations that return anothersequence (usually an IEnumerable or IQueryable) use deferred execution,whereas operations that return a s<strong>in</strong>gle value use immediate execution.The operations that are widely available <strong>in</strong> LINQ are known as the standard queryoperators—let’s take a brief look at them now.STANDARD QUERY OPERATORSLINQ’s standard query operators are a collection of transformations that have well-understoodmean<strong>in</strong>gs. LINQ providers are encouraged to implement as many of these operatorsas possible, mak<strong>in</strong>g the implementation obey the expected behavior. This iscrucial <strong>in</strong> provid<strong>in</strong>g a consistent query framework across multiple data sources. Ofcourse, some LINQ providers may expose more functionality, and some of the operatorsmay not map appropriately to the target doma<strong>in</strong> of the provider—but at least theopportunity for consistency is there.<strong>C#</strong> 3 has support for some of the standard query operators built <strong>in</strong>to the languagevia query expressions, but they can always be called manually. You may be <strong>in</strong>terested toknow that VB9 has more of the operators present <strong>in</strong> the language: as ever, there’s atrade-off between the added complexity of <strong>in</strong>clud<strong>in</strong>g a feature <strong>in</strong> the language andthe benefits that feature br<strong>in</strong>gs. Personally I th<strong>in</strong>k the <strong>C#</strong> team has done an admirablejob: I’ve always been a fan of a small language with a large library beh<strong>in</strong>d it.We’ll see some of these operators <strong>in</strong> our examples as we go through this chapter andthe next, but I’m not aim<strong>in</strong>g to give a comprehensive guide to them here: this book isprimarily about <strong>C#</strong>, not the whole of LINQ. You don’t need to know all of the operators<strong>in</strong> order to be productive <strong>in</strong> LINQ, but your experience is likely to grow over time. Theappendix gives a brief description of each of the standard query operators, and MSDNgives more details of each specific overload. When you run <strong>in</strong>to a problem, check thelist: if it feels like there ought to be a built-<strong>in</strong> method to help you, there probably is!Hav<strong>in</strong>g mentioned examples, it’s time to <strong>in</strong>troduce the data model that most ofthe rest of the sample code <strong>in</strong> this chapter will use.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!