Ordering Sequences

One of the most important consequences of Parallel LINQ (and, more generally, of parallel computing) is that processing is not done sequentially as it would happen on single-threaded code. This is because multiple threads run concurrently. To understand this, the following is an excerpt from the iteration on the result of the parallel query:

295315297317299319

You would probably expect something like the following instead, which is produced by the classic LINQ query:

295296297298299300

If you need to work in sequential order but do not want to lose the capabilities of PLINQ query, you can invoke the AsOrdered extension method that preserves the sequential order of the result, as demonstrated by the following code snippet:

Get Visual Basic 2015 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.