Conditionally use PLINQ with minimal code duplication [duplicate]

this is a sample join between two datatable by LINQ

 var JoinResult = (from p in dt.AsEnumerable()  
                   join t in dtTax.AsEnumerable()  
                   on p.Field<int>("Tax Id") equals t.Field<int>("Tax Id")  
                   select new  
                   {  
                       ProductName = p.Field<string>("Product Name"),  
                       BrandName = p.Field<string>("Brand Name"),  
                       ProductCategory = t.Field<string>("Product Category"),  
                       TaxCharge = t.Field<int>("Charge")   
                   }).ToList(); 

some time there could be huge amount of data in two datatable and some time two datatable has small amount of data. when data is small amount then AsParallel() not require because i know AsParallel() will slow down when run for small amount of data but when data volume is big then AsParallel() works good & fast.

please tell me how could i use AsParallel() dynamically as a result when data volume will be large and same way AsParallel() will not be applied when data volume will be small.

How to build dynamic query with expression tree for PLINQ

i found no good solution for my scenario to attach AsParallel() with my linq query dynamically. please share the know to achieve my goal. thanks


Solution 1:

Just make two execution branches, one of which uses AsParallel().

if (hugeAmountOfData)
{
    result = (
                 ...
                 query.AsParallel()
                 ...
             ).ToList();
}
else
{
    result = (
                 ...
                 query
                 ...
             ).ToList();
}

Solution 2:

Honestly I don't think it's possible. LINQ and PLINQ have different operators. LINQ operators operate on IEnumerable<T>s, while PLINQ operators operate on ParallelQuery<T>s. So although the two versions of the query (with and without AsParallel) look exactly identical, they are fundamentally two completely different queries.