Csharp/C Sharp/LINQ/Any
Содержание
Any operator with an string array
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {
public static void Main() { string[] presidents = {"G", "H", "a", "H", "over", "Jack"}; bool any = presidents.Any(); Console.WriteLine(any); }
}
</source>
Any with condition
<source lang="csharp"> using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{
public static void Main(){ int[] numbers = { 2, 6, 1, 5, 10 }; Console.WriteLine("Is there at least one odd number?"); Console.Write(numbers.Any(e => e % 2 == 1) ? "Yes, there is" : "No, there isn"t"); }
}
</source>
Any with false predicate
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {
public static void Main() { string[] presidents = {"G", "H", "a", "H", "over", "Jack"}; bool any = presidents.Any(s => s.StartsWith("A")); Console.WriteLine(any); }
}
</source>
Any with string operator
<source lang="csharp"> using System; using System.Linq; using System.Collections; using System.Collections.Generic; public class MainClass {
public static void Main() { string[] presidents = {"G", "H", "a", "H", "over", "Jack"}; bool any = presidents.Any(s => s.StartsWith("Z")); Console.WriteLine(any); }
}
</source>
Contains, Any return a bool value
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; public class MainClass {
public static void Main() { int[] numbers = { 10, 9, 8, 7, 6 }; bool hasTheNumberNine = numbers.Contains(9); // true bool hasMoreThanZeroElements = numbers.Any(); // true bool hasAnOddElement = numbers.Any(n => n % 2 == 1); // true }
}
</source>
Use Quantifiers: Any
<source lang="csharp"> using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass {
public static void Main() { string[] words = { "bei", "rie", "rei", "field" }; bool iAfterE = words.Any(w => w.Contains("ei")); Console.WriteLine("There is a word that contains in the list that contains "ei": {0}", iAfterE); }
}
</source>