O'Reilly logo

C# Multithreaded and Parallel Programming by Rodney Ringler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Ordering in PLINQ

Now, what if the order of our results matters? In a lot of cases, we will want the results to come back in a certain order. Let's examine how we can accomplish this.

There are several methods contained in the parallel extension methods of the IEnumerable interface. The two we use the most are AsOrdered() and AsOrderedBy(). These two methods guarantee order preservation in the results of the parallel query. This does add some cost and overhead.

Let's take our project from the last section and update it to return the ordered results.

How to do it

Let's open up our PLINQQuery project and make the following changes:

  1. Change the parallel query statement in the btnMethod1_Click method to look like the following by adding the AsOrdered method ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required