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+ 次浏览

开启你的 职业生涯

完成课程,获得认证

开始学习
广告
© . All rights reserved.