本文整理匯總了C#中System.Linq.Enumerable.All<TSource>方法的典型用法代碼示例。如果您正苦於以下問題:C# Enumerable.All<TSource>方法的具體用法?C# Enumerable.All<TSource>怎麽用?C# Enumerable.All<TSource>使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了Enumerable.All<TSource>方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: AllEx
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
public static void AllEx()
{
// Create an array of Pets.
Pet[] pets = { new Pet { Name="Barley", Age=10 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=6 } };
// Determine whether all pet names
// in the array start with 'B'.
bool allStartWithB = pets.All(pet =>
pet.Name.StartsWith("B"));
Console.WriteLine(
"{0} pet names start with 'B'.",
allStartWithB ? "All" : "Not all");
}
輸出:
Not all pet names start with 'B'.
示例2: AllEx2
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
class Person
{
public string LastName { get; set; }
public Pet[] Pets { get; set; }
}
public static void AllEx2()
{
List<Person> people = new List<Person>
{ new Person { LastName = "Haas",
Pets = new Pet[] { new Pet { Name="Barley", Age=10 },
new Pet { Name="Boots", Age=14 },
new Pet { Name="Whiskers", Age=6 }}},
new Person { LastName = "Fakhouri",
Pets = new Pet[] { new Pet { Name = "Snowball", Age = 1}}},
new Person { LastName = "Antebi",
Pets = new Pet[] { new Pet { Name = "Belle", Age = 8} }},
new Person { LastName = "Philips",
Pets = new Pet[] { new Pet { Name = "Sweetie", Age = 2},
new Pet { Name = "Rover", Age = 13}} }
};
// Determine which people have pets that are all older than 5.
IEnumerable<string> names = from person in people
where person.Pets.All(pet => pet.Age > 5)
select person.LastName;
foreach (string name in names)
{
Console.WriteLine(name);
}
/* This code produces the following output:
*
* Haas
* Antebi
*/
}