如何获得“件”列的总和在数据表中?
如何获得数据表中“件”列的总和?假设我有下表。如何计算article=“milk”和artno=“15”的“总”件数?
Columns: article artno pieces
Rows:
1 milk 15 1
2 water 12 1
3 apple 13 2
4 milk 15 1
5 milk 16 1
6 bread 11 2
7 milk 16 4
我的新数据表的结果应该是这样的:
Columns: article artno pieces
Rows:
1 bread 11 2
2 water 12 1
3 apple 13 2
4 milk 15 2
5 milk 16 5
我的代码:
foreach (DataRow foundDataRow in foundRows1)
{
int i = 0;
foreach (DataRow dataRow in foundRows2)
{
if (object.Equals(dataRow.ItemArray[0], foundDataRow.ItemArray[0])
&& object.Equals(dataRow.ItemArray[3], foundDataRow.ItemArray[3]))
{
i = i + Convert.ToInt16(dataRow.ItemArray[4]);
}
}
Debug.Print(i.ToString());
}
抱歉,但我是新的数据库开发人员,我的英语口语不太好。
How can I get a sum for the column "pieces" in a datatable? Say I had the following table. How can I calculate the "total" pieces for article="milk" and artno="15"?
Columns: article artno pieces
Rows:
1 milk 15 1
2 water 12 1
3 apple 13 2
4 milk 15 1
5 milk 16 1
6 bread 11 2
7 milk 16 4
The Result of the my new DataTable should be this:
Columns: article artno pieces
Rows:
1 bread 11 2
2 water 12 1
3 apple 13 2
4 milk 15 2
5 milk 16 5
My Code:
foreach (DataRow foundDataRow in foundRows1)
{
int i = 0;
foreach (DataRow dataRow in foundRows2)
{
if (object.Equals(dataRow.ItemArray[0], foundDataRow.ItemArray[0])
&& object.Equals(dataRow.ItemArray[3], foundDataRow.ItemArray[3]))
{
i = i + Convert.ToInt16(dataRow.ItemArray[4]);
}
}
Debug.Print(i.ToString());
}
Sorry, but i'm new DataBase developer and my english speak language is not so good.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
DataTable.Compute
< /a> 方法,你可以这样做:Using the
DataTable.Compute
method, you can do:您可以使用
AsEnumerable
方法,如下所示:You could use the
AsEnumerable
method like so:使用 Michael 的建议,减少 1 个步骤:(
这是使用 Linq to DataSet )
Using Michael's suggestion with 1 less step:
(This is using Linq to DataSet)
在 .NET 4 Windows 应用程序中,这段代码在 sum 变量中给出 9:
in a .NET 4 windows application, this snippet of code gives 9 in the sum variable: