CSharp - Parallel LINQ WithMergeOptions


WithMergeOptions operator controls how results are buffered as they are produced by the query.

By default, PLINQ creates a buffer that holds several result items and yields them to the result consumer when the buffer is full.


public static ParallelQuery<T> WithMergeOptions<T>(
          this ParallelQuery<T> source,
          ParallelMergeOptions mergeOptions

The ParallelMergeOptions enumeration has four values.

Item Meaning
NotBufferedeach result element to be yielded as it is produced.
FullyBuffered waits for all the results to be produced before they are yielded.
AutoBuffered lets the system select a buffer size and yield result elements when the buffer is full.
Default the same as AutoBuffered.

The following code contains an example of using the WithMergeOptions operator with the FullyBuffered value from the ParallelMergeOptions enumeration.


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

class Program//w  ww  .  j a  v  a  2 s.  co  m
    static void Main(string[] args)
        IEnumerable<int> results = ParallelEnumerable.Range(0, 10)
            .Select(i => {
                return i;

        Stopwatch sw = Stopwatch.StartNew();

        foreach (int i in results)
            Console.WriteLine("Value: {0}, Time: {1}", i, sw.ElapsedMilliseconds);

In this example, we have added a delay to the select clause of our query.

It takes a second for each item in the range sequence we created to be processed.

We then enumerate the query results and print out each item.

We use the Stopwatch class to timestamp each Console.WriteLine statement so we can see roughly how long it is between each result element being yielded.

Related Topics