易丰科技

标题: C# LINQ Any Example [打印本页]

作者: Calvin    时间: 2011-10-7 15:30
标题: C# LINQ Any Example
  1. class Pet
  2. {
  3.     public string Name { get; set; }
  4.     public int Age { get; set; }
  5.     public bool Vaccinated { get; set; }
  6. }
  7. public static void AnyEx2()
  8. {
  9.     // Create an array of Pets.
  10.     List<Pet> PetList =
  11.     {
  12.         new Pet { Name="Barley", Age=8, Vaccinated=true },
  13.         new Pet { Name="Boots", Age=4, Vaccinated=false },
  14.         new Pet { Name="Whiskers", Age=1, Vaccinated=false }
  15.     };
  16.     // Determine whether any pets over age 1 are also unvaccinated.
  17.     bool unvaccinated = PetList.Any(p => p.Age > 1 && p.Vaccinated == false);
  18.     Console.WriteLine(
  19.         "There {0} unvaccinated animals over age one.",
  20.         unvaccinated ? "are" : "are not any");
  21. }
复制代码






欢迎光临 易丰科技 (http://forum.yftek.com/) Powered by Discuz! X3