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