Hello friends,
In this article, I will explain to you below details of PLINQ
- What is Parallel LINQ (PLINQ) in C#
- What are the Benefits of PLINQ
- How to Write Parallel LINQ Queries
- What are the methods available in PLINQ
- Performance
- Example
- Parallel execution for a LINQ query by converting it into a PLINQ query.
- WithDegreeOfParallelism(int degreeOfParallelism): Specifies the maximum number of concurrently executing tasks that will be used by the query.
- WithExecutionMode(ParallelExecutionMode mode): Sets the execution mode of the PLINQ query to either ParallelExecutionMode.ForceParallelism or ParallelExecutionMode.Default.
- WithCancellation(CancellationToken cancellationToken): Specifies a cancellation token to enable cancellation of the PLINQ query.
- WithMergeOptions(ParallelMergeOptions options): Sets the merge options for combining results from parallel partitions (e.g., ParallelMergeOptions.NotBuffered or ParallelMergeOptions.AutoBuffered).
- WithOrdered(): Indicates that the output of the query should maintain the original order, even when parallelized.
- WithDegreeOfParallelism(int degreeOfParallelism): Sets the maximum number of concurrently executing tasks used by the query.
- WithExecutionMode(ParallelExecutionMode mode): Specifies the execution mode of the query (ForceParallelism or Default).
- WithCancellation(CancellationToken cancellationToken): Specifies a cancellation token for enabling query cancellation.
- WithMergeOptions(ParallelMergeOptions options): Sets the merge options for combining results from parallel partitions.
- ForAll(Action<TSource> action): Executes the specified action on each element of the query in parallel.
- AsSequential(): Returns a query that ensures subsequent operations are executed sequentially.
- AsUnordered(): Returns an unordered query that may execute more efficiently by ignoring the order of elements.
- Where<TSource>(Func<TSource, bool> predicate): Filters the elements of the query based on a specified condition.
- Select<TSource, TResult>(Func<TSource, TResult> selector): Projects each element of the query into a new form.
- OrderBy<TSource, TKey>(Func<TSource, TKey> keySelector): Sorts the elements of the query in ascending order based on a specified key.
- OrderByDescending<TSource, TKey>(Func<TSource, TKey> keySelector): Sorts the elements of the query in descending order based on a specified key.
- ThenBy<TSource, TKey>(Func<TSource, TKey> keySelector): Performs a secondary ascending sort on the elements based on a specified key.
- ThenByDescending<TSource, TKey>(Func<TSource, TKey> keySelector): Performs a secondary descending sort on the elements based on a specified key.
- GroupBy<TSource, TKey>(Func<TSource, TKey> keySelector): Groups the elements of the query based on a specified key.
- SelectMany<TSource, TResult>(Func<TSource, IEnumerable<TResult>> selector): Projects each element of the query into a sequence and flattens the resulting sequences into one sequence.
using System; using System.Linq; public class Program { public static void Main() { int[] numbers = Enumerable.Range(1, 1000000).ToArray(); var query = numbers.AsParallel() .Where(n => n % 2 == 0) .Select(n => n * n); Console.WriteLine("Squared even numbers: "); foreach (var result in query) { Console.Write($"{result}, "); } } }That’s all you need to do. PLINQ queries can be canceled using cancellation tokens, just like other tasks in TPL. Exception handling is also important when working with PLINQ, as exceptions might occur in parallel tasks.
Happy programming!!
Don’t forget to leave your feedback and comments below!
Regards
Sujeet Bhujbal
--------------------------------------------------------------------------------
Blog: www.sujeetbhujbal.com
Personal Website :-http://sujeetbhujbal.wordpress.com/
CodeProject:-http://www.codeproject.com/Members/Sujit-Bhujbal
CsharpCorner:-http://www.c-sharpcorner.com/Authors/sujit9923/sujit-bhujbal.aspx
Linkedin :-http://in.linkedin.com/in/sujitbhujbal
Twitter :-http://twitter.com/SujeetBhujbal
------------------------------------------------------------------------------
No comments:
Post a Comment