0

我有以下代码:

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++
  }

}

我的初始表可能包含数据:

abc, 50.0
abc, 50.0
def, 60.0
ghi, 70.0

当循环使用 for 时,abc 的 numberOfItems 应该 = 2,def 为 1,ghi 为 1。abc 的总金额应为 100.0,def 为 60.0,ghi 为 70.0。abc 应该是 1,def 应该是 2,ghi 应该是 3。

运行 for 循环时我注意到的第一件事是它跳过了第一个表中的一堆行,所以当我在 else 中创建第二个表时,numberOfItems 和 totalAmount 仍然为 0。第二个表应该类似于:

abc, 100, 2, 1
def, 60, 1, 2
ghi, 70, 1, 3

其中 csv 值中的最后一个数字是序列,之前的数字是每个帐户的 numberOfItems。

for (int i = 0; i < dt.Rows.Count; i++ )
            {
                DataRow row = dt.Rows[i];
                DataRow prevRow = i > 0 ? dt.Rows[i - 1] : null;

                if (prevRow != null && 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++;
                }
            }
4

4 回答 4

1

您可以使用DataTable.GetChanges

链接:http: //msdn.microsoft.com/en-us/library/k2552649.aspx

var changeTable = table.GetChanges();
foreach(var item in changeTable.Rows)
{
 .....

}

对于修改后的行,您可以使用参数DataRowState.Modified

table.GetChanges(DataRowState.Modified);
于 2012-09-19T15:12:57.263 回答
0

一行foreach不知道它是前一行。那么为什么不简单地使用 afor-loop呢?

for(int i = 0; i < dt.Rows.Count; i++)
{
    DataRow row = dt.Rows[ i ];
    DataRow prevRow = i > 0 ? dt.Rows[ i - 1 ] : null;

    if(prevRow != null && row["Account"] == prevRow["Account"])
    {
        // ...
    }
} 

现在您已经更新了您的问题,我建议您使用Linq-To-DataSet

var groups = dt.AsEnumerable()
    .GroupBy(r => r.Field<String>("Account"))
    .Select((g, i) => new { 
        Account = g.Key,
        TotalAmount = g.Sum(r => r.Field<double>("TotalAmount")),
        Count = g.Count(),
        Id = i + 1,
    });

foreach (var grp in groups)
    Console.WriteLine("{0},{1},{2},{3}"
        ,grp.Account, grp.TotalAmount, grp.Count, grp.Id);

请注意,您需要添加using System.Linq.

于 2012-09-19T15:21:01.063 回答
0
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 prevAccount == ""; //not sure how to handle this

foreach(DataRow row in dt.Rows)
{
   //I am stuck here
   if(row["Account"] == prevAccount)
   {
      numberOfItems++;
      totalAmount += Convert.ToDecimal(row["TotalAmount"]);
   }
   else
   {
       //reset everything and increase sequence
       numberOfItems = 0;
       totalAmount = 0m;
       sequence++;
    }
    prevAccount = row["Account"];
}
于 2012-09-19T15:19:46.707 回答
0
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 = null; //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++;
  }
  prevRow = row["Account"];

}

请注意,我放了一个 ; 序列递增后。

于 2012-09-19T15:15:46.113 回答