Home  >  Article  >  Backend Development  >  What is the difference between All and Any in C# Linq?

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

WBOY
WBOYforward
2023-09-03 17:01:03960browse

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(); }

Output

False
True

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();
}

Output if at least one element in the source sequence matches the supplied predicate

True
False

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete