TechGuy TechGuy - 2 months ago 8
ASP.NET (C#) Question

Summation/Add two values inside foreach loop

I want to add Array items inside forach loop.
My

RegionalEarn
array comes like this,

[0]Region1=25

[1]Region2=50


I need final
RModel.TAX
Should be
75(25+50)
But in my case it comes like
2550


My Code



string[] RegionalEarn = tickets["EARN"].ToString().Split(',');

foreach (var item in RegionalEarn)
{
RModel.TAX = RModel.TAX + item.Split('=')[1];
}

Answer

actually you done like concatenation.it comes only string values. so try convert to int or double .

 string[] RegionalEarn = tickets["EARN"].ToString().Split(',');


  foreach (var item in RegionalEarn)

   {

RModel.TAX = Convert.ToInt32(RModel.TAX) + Convert.ToInt32( item.Split('=')[1]);

} 
Comments