One operator that’s specific to PLINQ is called AsOrdered. Although sequences have an order for the contained elements, one often doesn’t care about preserving the original sequencing throughout a query. When this is the case, PLINQ can make many more optimizations than when it has to preserve the order of the original sequence.

Consider the following piece of classic LINQ to Objects code:

var res = from x in Enumerable.Range(0, 10000)          where x % 2 == 0          select x;foreach (var x in res)    Console.WriteLine(x);

This code returns the even numbers that appear in the source sequence, preserving the input order. So 0 comes before 2, 2 before 4, and so on. However, as soon as we add AsParallel ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.