Any(
) 方法返回 true。否则,返回 false。另一方面,如果源序列中的每个元素都与提供的谓词匹配,则 All(<predicate>) 方法返回 true。否则,返回 false</predicate></predicate></p>
<h2>示例</h2><pre class='brush:php;toolbar:false;'>static void Main(string[] args){
IEnumerable<double> doubles = new List<double> { 1.2, 1.7, 2.5, 2.4 };
bool result = doubles.Any(val => val < 1);
System.Console.WriteLine(result);
IEnumerable<double> doubles1 = new List<double> { 0.8, 1.7, 2.5, 2.4 };
bool result1 = doubles1.Any(val => val < 1);
System.Console.WriteLine(result1);
Console.ReadLine();
}Copy after login
Output
False
True
Copy after login
Example
static void Main(string[] args){
IEnumerable<double> doubles = new List<double> { 0.8, 0.9, 0.6, 0.7 };
bool result = doubles.All(val => val < 1);
System.Console.WriteLine(result);
IEnumerable<double> doubles1 = new List<double> { 0.8, 0.9, 1.0, 0.7 };
bool result1 = doubles1.All(val => val < 1);
System.Console.WriteLine(result1);
Console.ReadLine();
}
Copy after login
Output if at least one element in the source sequence matches the supplied predicate
True
False
Copy after login
The above is the detailed content of What is the difference between All and Any in C# Linq?. For more information, please follow other related articles on the PHP Chinese website!