C# 程式查詢序列的和
首先,設定一個序列。
List<int> myList = new List<int> { 1, 2, 3, 4 ,5};現在使用可查詢的 Sum() 方法來查詢總和。
myList.AsQueryable().Sum();
示例
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
List<int> myList = new List<int> { 1, 2, 3, 4 ,5};
Console.WriteLine("Sum of elements in a list...");
foreach (int res in myList) {
Console.WriteLine(res);
}
int sum = myList.AsQueryable().Sum();
Console.WriteLine("Sum = {0}", sum);
}
}輸出
Sum of elements in a list... 1 2 3 4 5 Sum = 15
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP