Search code examples
c#foreachdatatableiterator

How can I use foreach to look one variable ahead in the datatable with conditionals in C#?


You guys helped me out a lot with my last question and now I have another.

I am new to C# and I have to iterate through a datatable to count transactions for different states. However, I have to remove duplicate transactions. I have something similar in javascript where I took one variable ahead in the table and if it matches the current variable then it will subtract one from the counter.

How could I do this in C#? Here is what I am trying to far but with no success:

if (row["State"].ToString().Contains("CA")) //Looks for CA state in the "state" column, however, it appears to not be finding it?
            {
                californiaTransactions2021 += 1;
               if(row["InvNum"].ToString() == row["InvNum"]+1.ToString())
                {
                    californiaTransactions2021 -= 1;
                }

Here is what my datatable looks like: Datatable

As you can see, some invoice numbers are the same for California and they must be subtracted for the counter. What is the correct syntax for C# to do this in the loop?


Solution

  • If I needed to look ahead, and I needed to use foreach (rather than by index with for, which could use "index+1" to check the next row), then I would do something like this to preserve each row for one iteration and effectively trail my view of the current row by one cycle:

    DataRow cachedRow = null;
    
    foreach(var row in MyTable.Rows)
    {
        if (cachedRow != null)
        {
            var currentRow = cachedRow;
            var nextRow = row; 
            
            // Do whatever you want with these two rows
    
        }
        cachedRow = row;
    }
    // Don't forget to also check the final row (cachedRow) here
    

    Note that some iterators cycle through and return a mutation of the same object for each iteration. In this situation you'll need to make sure you're making a deep copy when you set the cachedRow.