Parallel Language Integrated Query

  1. How to convert a sequential query into a parallel
  2. How to convert a
    parallel query back into a sequential query?
  3. How to make the
    query’s result processed in parallel?
  4. What are the methods
    to control how the query is processed?

To convert a sequential query into parallel query we can use one of these

Name Description
AsParallel <(Of <( TSource >)>) (IEnumerable <(Of <(TSource >)>)) Enables parallelization of a query.
AsParallel(IEnumerable) Enables parallelization of a query.
AsParallel <(Of <(TSource >)>)(Partitioner <(Of <( TSource >)>)) Enables parallelization of a query, as sourced
by a custom partitioner that is responsible for splitting the input sequence
into partitions.

Sample 1:

static void SearchForObsoleteMethodsAsParallel(Assembly asm)


    (from type in asm.GetExportedTypes().AsParallel().WithMergeOptions(ParallelMergeOptions.NotBuffered)

member in type.GetMembers()

obsoleteAttribute = typeof(ObsoleteAttribute)

member.IsDefined(obsoleteAttribute, true)

                    select new { Name = member.Name, Type =
member.MemberType.ToString() })


                    .ForAll(item => Console.WriteLine(item.Type + “:” + item.Name ));


To convert a parallel query back into sequential query we can use

public static IEnumerable<TSource> AsSequential<TSource>(

    this ParallelQuery<TSource> source


        To make the query’s result processed in parallel we can use

public staticvoid ForAll<TSource>(

    this ParallelQuery<TSource> source,

    Action<TSource> action


            To control how to query is processed we can use methods such as:

public static ParallelQuery<TSource> WithCancellation<TSource>(

    this ParallelQuery<TSource> source,

    CancellationToken cancellationToken


public static ParallelQuery<TSource> WithDegreeOfParallelism<TSource>(

    this ParallelQuery<TSource> source,

    int degreeOfParallelism


public static ParallelQuery<TSource> WithExecutionMode<TSource>(

    this ParallelQuery<TSource> source,

    ParallelExecutionMode executionMode


public enum ParallelExecutionMode

Default – This
is the default setting. PLINQ will examine the query’s structure and will only
parallelize the query if will likely result in speedup. If the query structure
indicates that speedup is not likely to be obtained, then PLINQ will execute
the query as an ordinary LINQ to Objects query.

ForceParallelism – Parallelize
the entire query, even if that means using high-overhead algorithms. Use this flag
in cases where you know that parallel execution of the query will result in
speedup, but PLINQ in the Default mode would execute it as sequential.

public static ParallelQuery<TSource> WithMergeOptions<TSource>(

    this ParallelQuery<TSource> source,

    ParallelMergeOptions mergeOptions


public enum ParallelMergeOptions

Default - Use the default merge type, which is AutoBuffered.

NotBuffered - Use a merge without output buffers. As soon as result elements have been computed, make that element available to the consumer of the query.

AutoBuffered - Use a merge with output buffers of a size chosen by the system. Results will accumulate into an output buffer before they are available to the consumer of the query.