Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does PLINQ preserve the original order in a sequence?

Tags:

c#

plinq

Is PLINQ guaranteed to return query results in the order of the original sequence being operated on, even if 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 the result always be "aa, ba, ca, da, "?

like image 808
tbischel Avatar asked Jan 06 '11 23:01

tbischel


1 Answers

You have to use AsOrdered()to preserve the 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: How to: Control Ordering in a PLINQ Query

like image 68
BrokenGlass Avatar answered Nov 15 '22 04:11

BrokenGlass