| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Control Ordering in a PLINQ Query |
03/30/2017 |
.net |
dotnet-standard |
article |
|
c67eccc7-004d-4b2f-987e-919cbbd62ef7 |
10 |
rpetrusha |
ronpet |
wpickett |
How to: Control Ordering in a PLINQ Query
These examples show how to control the ordering in a PLINQ query by using the xref:System.Linq.ParallelEnumerable.AsOrdered%2A extension method.
[!WARNING] These examples are primarily intended to demonstrate usage, and may or may not run faster than the equivalent sequential LINQ to Objects queries.
Example
The following example preserves the ordering of the source sequence. This is sometimes necessary; for example some query operators require an ordered source sequence to produce correct results.
[!code-csharpPLINQ#12] [!code-vbPLINQ#12]
Example
The following example shows some query operators whose source sequence is probably expected to be ordered. These operators will work on unordered sequences, but they might produce unexpected results.
[!code-csharpPLINQ#14] [!code-vbPLINQ#14]
To run this method, paste it into the PLINQDataSample class in the PLINQ Data Sample project and press F5.
Example
The following example shows how to preserve ordering for the first part of a query, then remove the ordering to increase the performance of a join clause, and then reapply ordering to the final result sequence.
[!code-csharpPLINQ#15] [!code-vbPLINQ#15]
To run this method, paste it into the PLINQDataSample class in the PLINQ Data Sample project and press F5.
See Also
xref:System.Linq.ParallelEnumerable
Parallel LINQ (PLINQ)