CSharp - Parallel LINQ WithDegreeOfParallelism

Introduction

The WithDegreeOfParallelism operator sets an upper limit of the number of partitions that will be processed at once by PLINQ.

PLINQ breaks up your source sequence into partitions, which are then processed simultaneously.

Prototypes

public static ParallelQuery<T> WithDegreeOfParallelism<T>(
          this ParallelQuery<T> source,
          int degreeOfParallelism
)

Demo

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

class Program/*ww  w. j a v a2s  .  c  om*/
{
    static void Main(string[] args)
    {
          string[] codeNames = {"Python", "Java", "Javascript", "Bash", "C++", "Oracle"};
    
          IEnumerable<string> results = codeNames
              .AsParallel()
              .WithDegreeOfParallelism(2)
              .Where(p => p.Contains('o'))
              .Select(p => p);
    
          foreach (string president in results) {
              Console.WriteLine("Match: {0}", president);
          }
    }
}

Result