C# 程序根据谓词筛选数组元素
设置一个数组。
int[] arr = { 40, 42, 12, 83, 75, 40, 95 };使用 Where 子句和谓词获取大于 50 的元素。
IEnumerable<int> myQuery = arr.AsQueryable() .Where((a, index) => a >= 50);
我们来看看完整的代码 −
示例
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int[] arr = { 40, 42, 12, 83, 75, 40, 95 };
Console.WriteLine("Array:");
foreach (int a in arr) {
Console.WriteLine(a);
}
// getting elements above 70
IEnumerable<int> myQuery = arr.AsQueryable() .Where((a, index) => a >= 50);
Console.WriteLine("Elements above 50...:");
foreach (int res in myQuery) {
Console.WriteLine(res);
}
}
}输出
Array: 40 42 12 83 75 40 95 Elements above 50...: 83 75 95
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP