LINQ’s AsParallel returns ParallelQuery. I wonder if it’s possible to change this behavior so that I could compare the LINQ statement run with and without parallelism without actually changing the code? This behavior should be similar to Debug.Assert – when DEBUG preprocessor directive is not set, it’s optimized out. So I’d like to be able to make AsParallel return the same type without converting it to ParallelQuery.
I suppose I can declare my own extension method (because I can’t override AsParallel) and have that preprocessor directive analyzed within it:
public static class MyExtensions
{
#if TURN_OFF_LINQ_PARALLELISM
public static IEnumerable<T> AsControllableParallel<T>(this IEnumerable<T> enumerable)
{
return enumerable;
}
#else
public static ParallelQuery<T> AsControllableParallel<T>(this IEnumerable<T> enumerable)
{
return enumerable.AsParallel();
}
#endif
}
I wonder if there is any other way. Am I asking for too much?
What about