C# Linq Sum() 方法

csharpprogrammingserver side programming更新于 2025/9/14 5:27:00

使用 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

相关文章