Does PLINQ keep the original order in sequence?

Is PLINQ a guaranteed return of query results in the working order of the original sequence, even if the results are produced in parallel? For instance:

new List<String>(){"a", "b", "c", "d"}.asParallel().Select(str => str + "a").asSequential().ToList().ForEach(str => Console.Write(str + ", ");

will always be the result of "aa, ba, ca, da,"?

+5
source share
1 answer

You should use AsOrdered()to keep order:

        new List<String>(){"a", "b", "c", "d"}
            .AsParallel()
            .AsOrdered()
            .Select(str => str + "a")
            .AsSequential()
            .ToList()
            .ForEach(str => Console.Write(str + ", "));

Also check out this: Practical Guide. Order Management in PLINQ Query

+8
source

All Articles