【发布时间】:2012-09-20 02:09:19
【问题描述】:
我有以下代码循环通过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,但不处理 def。
dt2 应包含:
abc, 1, 2, 8, 2 def, 1, 3, 6, 1
其中 8 是 dt 中第 4 列的总数,2 是 abc 行数。
我只是得到这个
abc, 1, 2, 8, 2
【问题讨论】:
-
定义“进程”,因为您只在 dt2 中获取前两行的行?
-
@JamesMichaelHare - 是的,基本上 dt2 是 dt 的摘要。我会更新我的帖子