C# 程式檢查序列元素是否滿足某個條件
利用 All() 方法,來檢測一個序列的元素是否滿足某個條件。即使某個元素不滿足條件,All() 方法也將返回 False。
為了制定條件,需要使用 Lambda 表示式。下面是一個判斷所有元素是否都大於 20 的條件:
myArr.AsQueryable().All(val => val > 20);
我們看個例子。
示例
using System;
using System.Linq;
class Demo {
static void Main() {
int[] myArr = {7, 15, 22, 30, 40};
// checking if all the array elements are greater than 20
bool res = myArr.AsQueryable().All(val => val > 20);
Console.WriteLine(res);
}
}輸出
False
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP