O'Reilly logo

Learning .NET High-performance Programming by Antonio Esposito

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Data parallelism with PLINQ

PLINQ is the framework required to use LINQ within the TPL parallel framework. In .NET, it is straightforward to use parallelism against any LINQ query because we simply need to add the AsParallel method at the root of the query to switch the execution from the simple LINQ engine to PLINQ with TPL support.

The following example will execute two different where conditions against an enumerable using PLINQ:

static void Main(string[] args) { //a dataset var items = Enumerable.Range(1, 100); //multi-level in-memory where executed as data parallelism var processedInParallel = items.AsParallel() .Where(x => CheckIfAllowed1(x)) .Where(x => CheckIfAllowed2(x)) .ToArray(); Console.ReadLine(); } private static bool CheckIfAllowed2(int ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required