7

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, "?

tbischel
  • 6,337
  • 11
  • 51
  • 73

1 Answers1

11

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

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335