Csharp/CSharp Tutorial/LINQ/Any

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Any method

<source lang="csharp">using System; using System.Collections.Generic; using System.Linq; using System.Text;

   class Customer
   {
       public string ID { get; set; }
       public string City { get; set; }
       public string Country { get; set; }
       public string Region { get; set; }
       public decimal Sales { get; set; }
   }
   class Program
   {
       static void Main(string[] args)
       {
           List<Customer> customers = new List<Customer> {
             new Customer { ID="P", City="Tehran", Country="Iran", Region="Asia", Sales=7000 },
             new Customer { ID="Q", City="London", Country="UK", Region="Europe", Sales=8000 },
             new Customer { ID="R", City="Beijing", Country="China", Region="Asia", Sales=9000 },
             new Customer { ID="T", City="Lima", Country="Peru", Region="South America", Sales=2002 }
          };
           bool anyUSA = customers.Any(c => c.Country == "USA");
           Console.WriteLine(anyUSA);
       }
   }</source>

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>