title: How to check if a DataTable row(s) has changed when using foreach?
body: <p>I have the following code:</p>
  
  <pre><code>int sequence = 1; //This is incremented every time the Account changes
  int numberOfItems; //This is reset after the Account changes
  decimal totalAmount = 0m; //This is reset after the Account changes
  string prevRow; //not sure how to handle this
  
  foreach(DataRow row in dt.Rows)
  {
    //I am stuck here
    if(row["Account"]) == prevRow)
    {
    numberOfItems++;
    totalAmount += Convert.ToDecimal(row["TotalAmount"]);
    }
    else
    {
       //reset everything and increase sequence
       numberOfItems = 0;
       totalAmount = 0m;
       sequence++
    }
  
  }
  </code></pre>
  
  <p>My initial table may contain the data:</p>
  
  <p>abc, 50.0<br>
  abc, 50.0<br>
  def, 60.0<br>
  ghi, 70.0 </p>
  
  <p>When looping through using for, the numberOfItems for abc should = 2, 1 for def, and 1 for ghi.  The totalAmount for abc should be 100.0, 60.0 for def and 70.0 for ghi.  The sequence should be 1 for abc, 2 for def, and 3 for ghi.</p>
  
  <p>First thing I noticed when running the for loop is that it is skipping a bunch of rows in the first table, so when I create a second table in the else,  numberOfItems and totalAmount are still 0.  The second table should be something like:</p>
  
  <p>abc, 100, 2, 1<br>
  def, 60, 1, 2<br>
  ghi, 70, 1, 3  </p>
  
  <p>where the last number in the csv values is the sequence and the number right before that is the numberOfItems per account.</p>
  
  <pre><code>for (int i = 0; i &lt; dt.Rows.Count; i++ )
              {
                  DataRow row = dt.Rows[i];
                  DataRow prevRow = i &gt; 0 ? dt.Rows[i - 1] : null;
  
                  if (prevRow != null &amp;&amp; row[1] == prevRow[1])
                  {
                      numberOfItems++;
                      totalAmount += Convert.ToDecimal(row[2]);
                      row[3] = "D";
                      row[4] = c;
                      row[5] = sequence;
  
                  }
                  else
                  {
                      dt2.Rows.Add("N",
                          c,
                          row[1],
                          row[2],
                          row[3],
                          numberOfItems,
                          totalAmount,
                          sequence);
  
                      numberOfItems = 0;
                      totalAmount = 0m;
                      sequence++;
                  }
              }
  </code></pre>