Csharp/CSharp Tutorial/LINQ/Any

Материал из .Net Framework эксперт
Версия от 12:18, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Any method

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

Any operator with an string array

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

Any with condition

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

Any with false predicate

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

Any with string operator

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

Contains, Any return a bool value

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
    }
}

Use Quantifiers: Any

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