物件屬于呼叫外部物件類的串列
我想簡化以下代碼行
int TotalObjectVarN3 = (Object1.variable3 Object2.variable3 Object3.variable3 Object3.variable3);
uj5u.com熱心網友回復:
這種超負荷的System.Linq.Enumerable.Sum應該很好地為你作業:
計算 Int32 值序列的總和,這些值是通過對輸入序列的每個元素呼叫變換函式而獲得的。
在這種情況下,“轉換函式”將是Func<TSource, Int32>回傳variable3項的屬性值(例如obj => obj.variable3)。
你可以這樣使用它:
int TotalObjectVarN3 = new []{ Object1, Object2, Object3 }.Sum(obj => obj.variable3);
uj5u.com熱心網友回復:
這個是用 LINQ 做的
int TotalObjectVarN3 = MyList.Sum(o => o.variable3):
uj5u.com熱心網友回復:
UsingSelect是一個選項,然后是Sum方法。選擇 將序列的每個元素投影到新形式中。
list.Select(i => i.variable3).Sum();
更多關于Select- https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=net-5.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/361591.html
