Ordered PLINQ ForAll

后端 未结 6 1288
长情又很酷
长情又很酷 2021-01-04 08:09

The msdn documentation about order preservation in PLINQ states the following about ForAll().

  • Result when the source sequence is ordered
6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-04 08:21

    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.

提交回复
热议问题