10

I am looking at this code

var numbers = Enumerable.Range(0, 20);
var parallelResult = numbers.AsParallel().AsOrdered()
    .Where(i => i % 2 == 0).AsSequential();

foreach (int i in parallelResult.Take(5))
    Console.WriteLine(i);

The AsSequential() is supposed to make the resulting array sorted. Actually it is sorted after its execution, but if I remove the call to AsSequential(), it is still sorted (since AsOrdered()) is called.

What is the difference between the two?

comecme
  • 6,086
  • 10
  • 39
  • 67
MaPi
  • 1,601
  • 3
  • 31
  • 64
  • There is no "resulting array" as this example in the book does not use `.ToArray()` (I suppose you are referring to [Programming in C# Exam Ref 70-483](https://www.microsoftpressstore.com/store/exam-ref-70-483-programming-in-c-sharp-mcsd-9780735676824)). – comecme Sep 11 '16 at 13:29

2 Answers2

12

AsSequential is just meant to stop any further parallel execution - hence the name. I'm not sure where you got the idea that it's "supposed to make the resulting array sorted". The documentation is pretty clear:

Converts a ParallelQuery into an IEnumerable to force sequential evaluation of the query.

As you say, AsOrdered ensures ordering (for that particular sequence).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • The book I am referring to says: "If you have a complex query that can benefit from parallel processing but also has some parts that should be done sequentially, you can use the AsSequential to stop your query from being processed in parallel. One scenario where this is required is to preserve the ordering of your query. Listing 1-25 shows how you can use the AsSequential operator to make sure that the Take method doesn’t mess up your order." I checked your link and you are absolutely right. – MaPi Oct 09 '13 at 07:39
  • @user1648371: Sounds like a problem in the book, yes. – Jon Skeet Oct 09 '13 at 07:41
  • The [specs](https://msdn.microsoft.com/en-us/library/dd460680(v=vs.110).aspx) say `AsSequential` is used to preserve the ordering established by the previous clauses. Would it only work on SQL queries? The [specs](https://msdn.microsoft.com/en-us/library/dd997399(v=vs.110).aspx#Anchor_1) also state that `Take` will prevent the runtime from using parallelism anyway. So is the book wrong or not? I'm confused. – comecme Sep 11 '16 at 13:36
  • @comecme: Look at the example - the ordering is established by an OrderBy call. AsSequential preserves that order, but doesn't "make the resulting array sorted" as the original question claims. There's a big difference between preserving existing order and imposing a specific ordering. – Jon Skeet Sep 11 '16 at 13:42
  • @JonSkeet: If the OP is reading the book I am reading, the book doesn't really say that `AsSequential` will order the results. It states it will **preserve** the order. That is also what the specs say about `AsSequential`. So the question remains as to _why_ the results are still the same if you leave out `AsSequential`. – comecme Sep 11 '16 at 13:53
  • 1
    @comecme: Well I was answering the question asked :) But I'll expand the answer later tonight, if I have time. – Jon Skeet Sep 11 '16 at 13:56
2

I know that this was asked over a year old but here are my two cents.

In the example exposed, i think it uses AsSequential so that the next query operator (in this case the Take operator) it is execute sequentially.

However the Take operator prevent a query from being parallelized, unless the source elements are in their original indexing position, so that is why even when you remove the AsSequential operator, the result is still sorted.

Subos
  • 89
  • 4