The msdn documentation about order preservation in PLINQ states the following about ForAll()
.
AsOrdered()
wouldn't change anything - if you want to enforce order on the result of a parallel query you can simply use foreach()
ForAll()
is there to take advantage of parallelism, that means executing the side effect on more than one item in the collection at a time. In fact ordering only applies to the results of a query (the order of items in the result collection), but this has nothing to do with ForAll()
, since ForAll()
does not affect the order at all.
In PLINQ, the goal is to maximize performance while maintaining correctness. A query should run as fast as possible but still produce the correct results. In some cases, correctness requires the order of the source sequence to be preserved
Note that ForAll()
is not transforming the collection (it's not i.e projecting to a new collection), it's purely for executing side effects on the results of a PLINQ query.