限定符运算返回一个 Boolean 值,该值指示序列中是否有一些元素满足条件或是否所有元素都满足条件。
方法名 | 说明 | C# 查询表达式语法 | Visual Basic 查询表达式语法 | 更多信息 |
---|---|---|---|---|
全部 | 确定是否序列中的所有元素都满足条件。 | 不适用。 | Aggregate … In … Into All(…) | |
Any | 确定序列中是否有元素满足条件。 | 不适用。 | Aggregate … In … Into Any() | |
Contains | 确定序列是否包含指定的元素。 | 不适用。 | 不适用。 |
ALL的用法
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");
}
// This code produces the following output:
//
// Not all pet names start with 'B'.