Namespace: System.Linq.Parallel
Provides types and interfaces for implementing parallel LINQ (PLINQ) queries.
Classes
Name | Description |
---|---|
ParallelEnumerable<TSource> |
Represents a data source that supports parallel query operations. This is the primary type for PLINQ. |
ParallelQuery<TSource> |
Represents a data source that supports parallel query operations. |
ParallelQuerySource<TSource> |
Represents a data source for parallel LINQ queries. |
Interfaces
Name | Description |
---|---|
IParallelEnumerable<TSource> |
Interface for parallel enumerables. |
IParallelQuery<TSource> |
Interface for parallel queries. |
Extension Methods
These methods extend ParallelEnumerable<TSource>
and ParallelQuery<TSource>
to enable parallel query operations.
Name | Description |
---|---|
AsOrdered() |
Returns a new ParallelQuery<TSource> that preserves the order of the source sequence. |
AsUnordered() |
Returns a new ParallelQuery<TSource> that preserves the order of the source sequence. |
ForAll<TSource>(Action<TSource> action) |
Executes an operation on each element of a data source in parallel. |
WithDegreeOfParallelism(ParallelOptions options) |
Specifies execution options for a query, such as the maximum degree of parallelism. |
WithExecutionMode(ParallelExecutionMode executionMode) |
Specifies the execution mode (default or explicitly sequential) for a query. |
Example Usage
Here's a simple example demonstrating the use of PLINQ to process a collection in parallel:
using System;
using System.Collections.Generic;
using System.Linq;
public class Example
{
public static void Main(string[] args)
{
var numbers = Enumerable.Range(1, 1000);
var parallelQuery = numbers.AsParallel()
.Where(n => n % 2 == 0)
.Select(n => n * 2);
Console.WriteLine("Calculating even numbers multiplied by 2 in parallel...");
foreach (var result in parallelQuery)
{
Console.WriteLine(result);
}
// Using ForAll for side effects
parallelQuery.ForAll(result => Console.WriteLine($"Processed: {result}"));
}
}