Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo Enumerable.AsParallel(), going back to serial mode

Tags:

c#

linq

plinq

Assume you have a LINQ query like

source.AsParallel().Where(expensiveOperation).Select(cheapOperation)

I suppose in this case Select also runs in parallel execution mode. Maybe it's just a cheap operation like i => i*2, so is there a way to stop parallel execution at a point of querying with chained methods?

(maybe like .AsParallel().Where(expensiveOp).AsSerial?().Select(cheapOp)?)

like image 664
ahmet alp balkan Avatar asked Dec 23 '13 22:12

ahmet alp balkan


1 Answers

The operation you're looking for is AsSequential.

source.AsParallel().Where(expensiveOp).AsSequential().Select(cheapOp)
like image 121
Douglas Avatar answered Nov 06 '22 19:11

Douglas