2

How can I implement the below statement using a foreach loop

for(int i = 0; i<=dt.Rows.Count -1; i++)
{
  var quote = Convert.ToDouble(dt.Rows[i]["Data"]) - Convert.ToDouble(dt.Rows[i-1]["Data"])

}

when I use a foreach loop:

foreach (DataRow row in dt.Rows)
{
       // how can i write this statement like the loop above. 
}
devlin carnate
  • 7,384
  • 7
  • 43
  • 71
ekrem tapan
  • 147
  • 2
  • 13

2 Answers2

1

You can store the previous value on each iteration of the loop to use in the next iteration.

var prev = 0;
foreach (DataRow row in dt.Rows)
{
     var quote = Convert.ToDouble(row["Data"]) - Convert.ToDouble(prev);
     prev = row["Data"];
}

How to set quote with an initial value of 0:

double quote = 0.0;
var prev = 0;
foreach (DataRow row in dt.Rows)
{
     quote = Convert.ToDouble(row["Data"]) - Convert.ToDouble(prev);
     prev = row["Data"];
}

Another way to set quote to 0 on the first iteration of the loop:

var prev = 0;
var i = 0;
foreach (DataRow row in dt.Rows)
{
     double quote;
     if(i == 0) 
     {
          quote = 0.0;
     } 
     else 
     {
          quote = Convert.ToDouble(row["Data"]) - Convert.ToDouble(prev);
     }
     prev = row["Data"];
     i++;
}

NOTE: this last example, based on OP's comments to my answer, is really just a stab in the dark because the OP has given no clear definition of what is needed. These add-on examples are really beyond the scope of the original question.

devlin carnate
  • 7,384
  • 7
  • 43
  • 71
  • at the first time if i need to take quote value for "0" how can i set it ? – ekrem tapan May 24 '16 at 15:40
  • If you need to set quote to 0 at the beginning, then declare it outside the loop, like I've shown with the variable `prev`. – devlin carnate May 24 '16 at 15:42
  • u r wrong, just u r declare quote = 0.0 but i mean when loop is starting first loop values of quote how can i take 0 ? – ekrem tapan May 24 '16 at 15:58
  • @ekremtapan - I have no idea what you're asking. If you want quote to be set to 0 when starting the first loop, just do it outside the loop, like I've shown. If for some reason that doesn't fit your scenario, then you'll need to create a variable outside the loop that tracks what iteration you're on. I'll update my answer with that scenario, too. – devlin carnate May 24 '16 at 16:01
  • @ekremtapan - I'm done adding examples. If you have further requirements, you need to be far more specific with regards to what you're looking for. At this point, I've answered your original question. These other "what if" scenarios you're tacking on in comments are out of scope from what you originally asked. – devlin carnate May 24 '16 at 16:08
  • 1
    @ devlin carnate Thank you so much for your time and effort to assist me – ekrem tapan May 24 '16 at 16:11
0

If you use a foreach-loop you don't have an index but only the current item. So if you want to access the previous DataRow you should use a for-loop as in your first approach. But you could write it more readable and efficient:

for(int i = 1; i < dt.Rows.Count -1; i+=2)
{
    double thisData = dt.Rows[i].Field<double>("Data");
    double prevData = dt.Rows[i-1].Field<double>("Data");
    double quote = thisData - prevData;
}
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859