O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

AsOrdered

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required