Converting LINQ queries to PLINQ
In this section, we will look at some additional PLINQ operators and show you how you can leverage them to turn existing LINQ queries into PLINQ queries. Your existing queries may have requirements for preserving the order of data. Perhaps your existing code doesn’t use LINQ at all. There could be an opportunity there to convert some logic in foreach
loops into PLINQ operations.
The way to convert a LINQ query to a PLINQ query is by inserting an AsParallel()
statement into the query, as we did in the previous section. Everything that follows AsParallel()
will run in parallel until an AsSequential()
statement is encountered.
If your queries require that the original order of objects be preserved, you can include an AsOrdered()
statement:
var results = people.AsParallel().AsOrdered()
.Where(p => p.LastName.StartsWith("H"));
However, this will not be as performant as queries that do not preserve...