How to use AsParallel conditionally or attach dynamically at runtime

T.Zacks 3,986 Reputation points
2021-04-03T17:51:51.26+00:00

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.

https://stackoverflow.com/questions/22928082/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

EDIT

I am looking for some dynamic behavior. when joining two small resultset and use AsParallel then joining taking long time but when joining two large resultset and use AsParallel then execution getting faster.

Now i am using AsParallel conditionally this way which is not good for me.

if (dt.rows.count <=50000)
{
 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(); 

}
else
{
 var JoinResult = (from p in dt.AsEnumerable().AsParallel()  
    join t in dtTax.AsEnumerable().AsParallel()  
    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(); 
}

how could i develop a something which will detect result is large and then use AsParallel else not use AsParallel. can you please give me any direction to attach AsParallel dynamically ?

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,279 questions
{count} votes