C# - How do I detect a change in one column between the current DataRow and the Next?

Viewed 29

I have a query over two tables, SchoolBus and SchoolPupil:

Select SB.BusNumber, SB.BusDriverLastName, SB.BusDriverFirstName, SP.LastName, SP.FirstName, SP.HomeTown, SP.Age, SP.BusFare 
From SchoolBus As SB 
Inner Join SchoolPupil As SP 
On SP.BusNumber = SB.BusNumber 
Order By SB.BusNumber, SP.HomeTown 

I need to read that query in my C# application and do rather complicated computations on home town and bus fare for each bus number. That is, for each value of BusNumber, I need to work on a set of all the data rows with that value.

Which means I need to detect the last row with a value of BusNumber.

It can be done rather simply:

for (int z_intIdx = 0; z_intIdx < z_dstData.Tables[0].Rows.Count; z_intIdx++)
{
    // do stuff with the DataRow and put it in an IEnumerable for later

    if (z_intIdx == z_dstData.Tables[0].Rows.Count - 1 ||
            z_dstData.Tables[0].Rows[z_intIdx] != z_dstData.Tables[0].Rows[z_intIdx + 1])
    {
        // do rather complications with the IEnumerable
    }
}

But I can't help feel that there might be a method or property in DataTable, DataRow, DataRowCollection or something else that would be more concise and would let me use a foreach or while loop.

So, is there something I could use instead of the above code?

0 Answers
Related