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.

290 CHAPTER 11 Query expressions and LINQ to Objectsforeach (str<strong>in</strong>g start <strong>in</strong> str<strong>in</strong>gs){Console.WriteL<strong>in</strong>e(start);}The output of list<strong>in</strong>g 11.6 is “Fir,” “Sec,” “Thi”—but what’s more <strong>in</strong>terest<strong>in</strong>g is thetranslated query expression, which islist.Cast().Select(entry => entry.Substr<strong>in</strong>g(0,3));Without the cast, we wouldn’t be able to call Select at all, because the extensionmethod is only def<strong>in</strong>ed for IEnumerable rather than IEnumerable. Even whenyou’re us<strong>in</strong>g a strongly typed collection, you might still want to use an explicitly typedrange variable, though. For <strong>in</strong>stance, you could have a collection that is def<strong>in</strong>ed to bea List but you know that all the elements are <strong>in</strong>stances ofMyImplementation. Us<strong>in</strong>g a range variable with an explicit type of MyImplementationallows you to access all the members of MyImplementation without manually <strong>in</strong>sert<strong>in</strong>gcasts all over the code.We’ve covered a lot of important conceptual ground so far, even though wehaven’t achieved any impressive results. To recap the most important po<strong>in</strong>ts briefly:■ LINQ is based on sequences of data, which are streamed wherever possible.■ Creat<strong>in</strong>g a query doesn’t immediately execute it: most operations use deferredexecution.■ Query expressions <strong>in</strong> <strong>C#</strong> 3 <strong>in</strong>volve a preprocess<strong>in</strong>g phase that converts theexpression <strong>in</strong>to normal <strong>C#</strong>, which is then compiled properly with all the normalrules of type <strong>in</strong>ference, overload<strong>in</strong>g, lambda expressions, and so forth.■ The variables declared <strong>in</strong> query expressions don’t act like anyth<strong>in</strong>g else: theyare range variables, which allow you to refer to data consistently with<strong>in</strong> thequery expression.I know that there’s a lot of somewhat abstract <strong>in</strong>formation to take <strong>in</strong>. Don’t worry ifyou’re beg<strong>in</strong>n<strong>in</strong>g to wonder if LINQ is worth all this trouble. I promise you that it is.With a lot of the groundwork out of the way, we can start do<strong>in</strong>g genu<strong>in</strong>ely usefulth<strong>in</strong>gs—like filter<strong>in</strong>g our data, and then order<strong>in</strong>g it.11.3 Filter<strong>in</strong>g and order<strong>in</strong>g a sequenceYou may be surprised to learn that these two operations are some of the simplest toexpla<strong>in</strong> <strong>in</strong> terms of compiler translations. The reason is that they always return asequence of the same type as their <strong>in</strong>put, which means we don’t need to worry aboutany new range variables be<strong>in</strong>g <strong>in</strong>troduced. It also helps that we’ve seen the correspond<strong>in</strong>gextension methods <strong>in</strong> chapter 10.11.3.1 Filter<strong>in</strong>g us<strong>in</strong>g a where clauseIt’s remarkably easy to understand the where clause. The format is justwhere filter-expressionLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!