Sử dụng phương thức Linq Average () để tìm giá trị trung bình của một chuỗi các giá trị số.
Đầu tiên, hãy thiết lập một trình tự.
List<int> list = new List<int> { 5, 8, 13, 35, 67 }; Bây giờ, hãy sử dụng phương thức Queryable Average () để lấy giá trị trung bình.
Queryable.Average(list.AsQueryable());
Ví dụ
using System;
using System.Linq;
using System.Collections.Generic;
class Demo {
static void Main() {
List<int> list = new List<int> { 5, 8, 13, 35, 67 };
double avg = Queryable.Average(list.AsQueryable());
Console.WriteLine("Average = "+avg);
}
} Đầu ra
Average = 25.6