score:1
Accepted answer
You're looking for ParallelEnumerable.AsOrdered
:
var result = sequence
.AsParallel()
.AsOrdered()
.Aggregate(seed: string.Empty, func: (prev, current) => prev + current);
The fact that you need to preserve ordering will have a performance hit on your query. As the results need to be aggregated in order, you won't be enjoying the maximum benefit of parallelism, and may sometimes lead to degraded performance over sequential iteration. Having said that, this will do what you're after.
For example, the following code will produce "[7][35][22][6][14]"
consistently:
var result = new [] { 35, 14, 22, 6, 7 }
.AsParallel()
.AsOrdered()
.Select(c => "[" + c + "]")
.Aggregate(seed: string.Empty, func: (prev, current) => prev + current);
Console.WriteLine(result);
There is a good post about PLINQ Ordering by the Parallel Programming Team.
Source: stackoverflow.com
Related Articles
- Is there an easy way to do parallel aggregation with a non-commutative operation?
- Is there any way to make Code Contracts work with LINQ?
- Is there a better way to achieve this with Linq or a better code
- Is there an easy way to join two ILists with joint key
- Entity Framework: There is already an open DataReader associated with this Command
- What does this C# code with an "arrow" mean and how is it called?
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- Is there something wrong with my System.Xml.Linq library?
- Is there an easy way to parse a (lambda expression) string into an Action delegate?
- Is there some sort of syntax error with this LINQ JOIN?
- Is there a good source that gives an overview of linq optimizations?
- Why doesn't this code compile in VS2010 with .NET 4.0?
- Is there an easy way to merge two ordered sequences using LINQ?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- Why is this code with PredicateBuilder not working?
- C# linq order by and other statements with foreach, is there a performance difference?
- Is there a JS library that supports writing linq to sql queries with nodejs?
- Can you advise me a resource with LINQ/lambda code exercises?
- Getting COUNT and SKIP TAKE in one operation with Linq to Entities
- LINQ Source Code Available
- LINQ: is there a way to supply a predicate with more than one parameter to where clause
- Using Linq is there a way to perform an operation on every element in an Array
- Is there an efficient way to do a selection statement with two variables?
- Linq with where clause in many-to-many EF Code First object
- LINQ GroupBy Aggregation with AutoMapper
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- .NET 4 Code Contracts: "requires unproven: source != null"
- DataTable select with LiNQ and check is there duplicate rows or not
- Is there a more clear and concise way to express this statement with better LINQ?
- Is there a way to speed up this code that finds data changes in two XML files?
- Dynamic WPF generation with Linq to XML
- How to read a section from web.config on IIS7 w/ .net 4 in C#
- Linq Anonymous type members must be declared in Sub Query
- Join dynamic query result with an EntitySet using Linq?
- Moving node in xdocument
- How to query nested json data in linq?
- Concatenate the last letter onto a strip of code (Linq?)
- Make (mostly) equal length sets
- Searching a DataTable for a DataType
- Select in Method Syntax C# - returns a collection of anonymous object
- Why is FxCop raising the error "Types that own disposable fields should be disposable" on a class with no disposable fields?
- LINQ request for getting the furthest xml child node with an attribute
- Code to collapse duplicate and semi-duplicate records?
- C# - from SQL to Linq - Left Outer Join/Inner Join
- Linq Query with Dictionary C#
- Applying LINQ filters based on a multi-dimensional array
- How to get multiple level deep line items using linq in c#?
- What are the query limitations of OData?
- Why is it better to use data structure in Entity Framework
- How do I cast a ASP.NET session to get back my list?