CSharp - Parallel LINQ AsOrdered

Introduction

The AsOrdered operator preserves the order of the results to match the order of the source sequence.

Prototypes

The AsOrdered operator has two prototypes.

public static ParallelQuery<T> AsOrdered<T>(
          this ParallelQuery<T> source
)

The first AsOrdered prototype enforces result ordering on a ParallelQuery<T>.

The result of the operator is also a ParallelQuery<T>, which you can then use as the input sequence for your PLINQ query.

The second AsOrdered prototype operated on the weakly typed ParallelQuery.

public static ParallelQuery AsOrdered(
          this ParallelQuery source
)

The second prototype operates on a weakly typed ParallelQuery.

This is the kind of ParallelQuery you get from a legacy collection.

You need to use the OfType or Cast operators before you can use the data sequence in a PLINQ query.

Demo

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

class Program//  www .j ava2s  .  c o  m
{
    static void Main(string[] args)
    {
              string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
        
              IEnumerable<string> results = codeNames
                  .AsParallel()
                  .AsOrdered()
                  .Where(p => p.Contains('o'))
                  .Select(p => p);
        
              foreach (string president in results) {
            Console.WriteLine("Match: {0}", president);
        }
    }
}

Result