本文介紹了C#對象列表,我如何獲得一個屬性的總和的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我有一個對象列表.單個對象條目的一個屬性是數量.如何獲得金額的總和?
I have a list of objects. One property of the individual object entry is amount. How do I get the sum of amount?
如果我的列表是 double 類型,我可以這樣做:
If my list was of type double I may be able to do something like this:
double total = myList.Sum();
但是我想要類似的東西,但是這個語法不正確.
However I want to something similar to this, yet this syntax is incorrect.
double total = myList.amount.Sum();
我應該如何去完成這個?如果可能的話,我希望使用 Sum 函數,而不是循環并計算值.
How should I go about accomplishing this? I would love to use the Sum function if possible instead of looping through and calculating the value.
推薦答案
using System.Linq;
...
double total = myList.Sum(item => item.Amount);
這篇關于C#對象列表,我如何獲得一個屬性的總和的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!