LINQ Interview Question #148

When should PLINQ be avoided?

Advanced LINQ, Practical Problems & Senior Interview Scenarios Senior Advanced

Quick Interview Answer

Avoid PLINQ for trivial operations, small collections, I/O-bound work, code with unsafe shared mutable state, or situations where strict ordering and deterministic side effects are essential.

Detailed Explanation

Avoid PLINQ for trivial operations, small collections, I/O-bound work, code with unsafe shared mutable state, or situations where strict ordering and deterministic side effects are essential.

Database IQueryable queries should not be made 'faster' by adding PLINQ. Let the database provider execute the database query, then consider parallel in-memory processing only when measurement demonstrates a genuine CPU-bound need.

Code Example

// Usually unnecessary for cheap work:
var result = numbers
    .Where(n => n > 0)
    .Select(n => n * 2)
    .ToList();

// Measure before replacing this with AsParallel().