Hary
Hary

Reputation: 5818

Automatic conversion with LINQ

My VS2010 using ReSharper which prompts to converts foreach to LINQ. It converts from

foreach (var item in quotePrice.ExtraServiceBreakdown)
{
    hazmatRate = (quoteRequest.IsHazMat && item.Id == VitranHazmatCode) ?   
                                                 item.Cost : hazmatRate;
}

to

hazmatRate = quotePrice.ExtraServiceBreakdown.Aggregate(
                 hazmatRate, (current, item) => 
                     (quoteRequest.IsHazMat && item.Id == VitranHazmatCode) ? 
                         item.Cost : current);

I have two questions here,

  1. What does current meant? Is that points to the variable hazmatRate?
  2. What does Aggregate actually does?

Upvotes: 1

Views: 129

Answers (1)

JMan
JMan

Reputation: 2629

  1. Current indeed point to your Hazmat
  2. LINQ Aggregate algorithm explained

And i think you will need to do as stated in the comment by Tim:

    hazmatRate += (quoteRequest.IsHazMat && item.Id == VitranHazmatCode) ?   item.Cost : hazmatRate;

But in this case i'd change 'hazmatRate' by a base rate declared before your function. Otherwise you would be increasing your value with the value it contained before making it grow exponentially

Upvotes: 1

Related Questions