本文介紹了如何計(jì)算 LINQ 中 DataTable 的列的總和(到數(shù)據(jù)集)?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
限時(shí)送ChatGPT賬號(hào)..
我剛剛開(kāi)始閱讀 LINQ,我想開(kāi)始將它整合到我的代碼中.我知道如何通過(guò)Foreach"遍歷行或在特定列上執(zhí)行 compute.sum 來(lái)計(jì)算 DataTable 列的總和.如何使用 LINQ to DataSet 進(jìn)行等效操作?
I'm just started to read up on LINQ and I want to start incorporating it into my code. I know how to compute the sum of a DataTable's column by either "Foreach"-ing through the rows or by doing a compute.sum on the specific column. How do I do the equivalent with LINQ to DataSet?
推薦答案
如果沒(méi)有類型(將 int
替換為正確的數(shù)據(jù)類型):
If untyped (replace int
with the correct data type):
var sum = table.AsEnumerable().Sum(x=>x.Field<int>(3));
或:
var sum = table.AsEnumerable().Sum(x=>x.Field<int>("SomeProperty"));
如果輸入:
var sum = table.Sum(x=>x.SomeProperty);
這篇關(guān)于如何計(jì)算 LINQ 中 DataTable 的列的總和(到數(shù)據(jù)集)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!