本文介紹了我怎樣才能得到列“pieces"的總和?在數據表中?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
如何獲得數據表中pieces"列的總和?假設我有下表.如何計算 article="milk" 和 artno="15" 的總"件數?
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
我的代碼:
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.
推薦答案
使用 DataTable.Compute
方法,你可以這樣做:
Using the DataTable.Compute
method, you can do:
int sum = (int)table.Compute("Sum(pieces)", "article = 'milk' AND artno='15'");
這篇關于我怎樣才能得到列“pieces"的總和?在數據表中?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!