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.

350 CHAPTER 12 LINQ beyond collectionsAsParallel call, we would only use a s<strong>in</strong>gle thread, but AsParallel and the result<strong>in</strong>gcalls to the ParallelEnumerable extension methods means that the work is split <strong>in</strong>toup to three threads. 12One caveat about this: unless you specify that you want the results <strong>in</strong> the sameorder as the str<strong>in</strong>gs <strong>in</strong> the orig<strong>in</strong>al sequence, PLINQ will assume you don’t m<strong>in</strong>d gett<strong>in</strong>gresults as soon as they’re available, even if results from earlier elements haven’tbeen returned yet. You can prevent this by pass<strong>in</strong>g QueryOptions.PreserveOrder<strong>in</strong>gas a parameter to AsParallel.There are other subtleties to us<strong>in</strong>g PLINQ, such as handl<strong>in</strong>g the possibility of multipleexceptions occurr<strong>in</strong>g <strong>in</strong>stead of the whole process stopp<strong>in</strong>g on the first problematicelement—consult the documentation for further details when ParallelExtensions is fully released. More examples of PLINQ are <strong>in</strong>cluded <strong>in</strong> the downloadablesource code.As you can see, PLINQ isn’t a “data source”—it’s a k<strong>in</strong>d of meta-provider, alter<strong>in</strong>ghow a query is executed. Many developers will never need it—but I’m sure that thosewho do will be eternally grateful for the coord<strong>in</strong>ation it performs for them beh<strong>in</strong>dthe scenes.These won’t be the only new providers Microsoft comes up with—we should expectnew APIs to be built with LINQ <strong>in</strong> m<strong>in</strong>d, and that should <strong>in</strong>clude your own code as well.I confidently expect to see some weird and wonderful uses of LINQ <strong>in</strong> the future.12.6 SummaryPhew! This chapter has been the exact opposite of most of the rest of the book.Instead of focus<strong>in</strong>g on a s<strong>in</strong>gle topic <strong>in</strong> great detail, we’ve covered a vast array of LINQproviders, but at a shallow level.I wouldn’t expect you to feel particularly familiar with any one of the specific technologieswe’ve looked at here, but I hope you’ve got a deeper understand<strong>in</strong>g of whyLINQ is important. It’s not about XML, or <strong>in</strong>-memory queries, or even SQL queries—it’sabout consistency of expression, and giv<strong>in</strong>g the <strong>C#</strong> compiler the opportunity to validateyour queries to at least some extent, regardless of their f<strong>in</strong>al execution platform.You should now appreciate why expression trees are so important that they areamong the few framework elements that the <strong>C#</strong> compiler has direct <strong>in</strong>timate knowledgeof (along with str<strong>in</strong>gs, IDisposable, IEnumerable, and Nullable, for example).They are passports for behavior, allow<strong>in</strong>g it to cross the border of the local mach<strong>in</strong>e,express<strong>in</strong>g logic <strong>in</strong> whatever foreign tongue is catered for by a LINQ provider.It’s not just expression trees—we’ve also relied on the query expression translationemployed by the compiler, and the way that lambda expressions can be converted toboth delegates and expression trees. Extension methods are also important, as withoutthem each provider would have to give implementations of all the relevant methods. If12 I’ve explicitly specified the number of threads <strong>in</strong> this example to force parallelism even on a s<strong>in</strong>gle-core system.If the number of threads isn’t specified, the system acts as it sees fit, depend<strong>in</strong>g on the number of coresavailable and how much other work they have.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!