What is the difference between All and Any in C# Linq?

WBOY
Release: 2023-09-03 17:01:03
forward
909 people have browsed it

C# Linq 中的 All 和 Any 有什么区别?

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!