C# Linq Where 方法
csharpprogrammingserver side programming更新于 2025/9/15 19:24:00
Where 方法根据谓词过滤值数组。
此处,谓词检查 70 以上的元素。
Where((n, index) => n =>= 70);
示例
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] arr = { 10, 30, 20, 15, 90, 85, 40, 75 };
Console.WriteLine("Array:");
foreach (int a in arr)
Console.WriteLine(a);
// 获取 70 以上的元素
IEnumerable myQuery = arr.AsQueryable().Where((n, index) => n >= 70);
Console.WriteLine("Elements above 70...:");
foreach (int res in myQuery)
Console.WriteLine(res);
}
}
输出
Array: 10 30 20 15 90 85 40 75 Elements above 70...: 90 85 75

