C# Linq Sum() 方法


使用Linq Sum()方法求和。

以下是包含整數元素的列表。

List<int> list = new List<int> { 99, 34, 77, 75, 87, 35, 88};

現在使用Sum()方法求和。

list.AsQueryable().Sum();

以下示例演示如何求和包含整數元素的列表中的元素。

示例

 線上演示

using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
   public static void Main() {
      List<int> list = new List<int> { 99, 34, 77, 75, 87, 35, 88};
      int res = list.AsQueryable().Sum();
      Console.WriteLine("Sum = {0}", res);
   }
}

輸出

Sum = 495

更新日期:2020 年 6 月 23 日

10K+ 次瀏覽

開啟你的 職業生涯

完成課程,獲得認證

開始學習
廣告