C# LINQ Where 方法


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);
      // getting elements above 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

更新时间: 2020 年 6 月 23 日

2K+ 观看次数

开启你的职业生涯

完成课程获得认证

入门
广告