我有以下代码循环通过DataTable
并建立另一个如果满足某些条件。但是,最初的DataTable中的最后一行将被跳过。为什么循环跳过我的DataTable中的最后一行?
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow row = dt.Rows[i];
DataRow nextRow = i < dt.Rows.Count - 1 ? dt.Rows[i + 1] : null;
string account = row[1].ToString();
string nextAccount = "";
if (nextRow != null)
{
nextAccount = nextRow[1].ToString();
}
numberOfItems++;
totalAmount += Convert.ToDecimal(row[2]);
row[4] = "D";
row[5] = c;
row[6] = Sequence;
if (nextRow != null && i < dt.Rows.Count && account != nextAccount)
{
dt2.Rows.Add("N",
c,
row[1],
row[2],
row[3],
numberOfItems,
totalAmount,
Sequence);
numberOfItems = 0;
totalAmount = 0m;
Sequence++;
}
}
在上面的代码,如果我有一个表,如:
abc, 1, 2, 3 abc, 1, 2, 5 def, 1, 3, 6
它会同时处理ABC的,但不是高清。
DT2应包含:
abc, 1, 2, 8, 2 def, 1, 3, 6, 1
凡8是总的第4列中dt和2为abc的行数。
我只得到这虽然
abc, 1, 2, 8, 2
定义“process”,因为在前两行中只有dt2中有行吗? –
@JamesMichaelHare - 是的,基本上dt2是dt的总结。我会更新我的帖子 – Xaisoft