question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked DanielZhang-MSFT edited

How to use AsParallel conditionally or attach dynamically at runtime

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

dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi TZacks-2728,
Performance depends on the target computer and the characteristics of the query and data source. As you said, if you have a computationally intensive query.
AsParallel might be helpful. And if you have more than two processor cores, the AsParallel call may increase performance even more.
As far as I know, it is twice as fast on a dual-core computer when using AsParallel.
So you can judge whether you need to use AsParallel according to your actual situation.
And using the LINQ Dynamic Query Library maybe helpful for you, more details please refer to this blog.
Best Regards,
Daniel Zhang


0 Votes 0 ·

please have a look at my Edit area and suggest something helpful. thanks

0 Votes 0 ·

Hi @TZacks-2728,
I am afraid it is difficult to attach AsParallel dynamically and LINQ and PLINQ have different operators.
Best Regards,
Daniel Zhang

0 Votes 0 ·

0 Answers