Queryable.All<TSource> 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
判斷序列的所有項目是否全都符合條件。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static bool All(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static bool All<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member All : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> bool
<Extension()>
Public Function All(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As Boolean
類型參數
- TSource
source
項目的類型。
參數
- source
- IQueryable<TSource>
要測試其項目是否符合條件的序列。
- predicate
- Expression<Func<TSource,Boolean>>
用來測試每個項目是否符合條件的函式。
傳回
如果來源序列的每個項目都通過以指定之述詞 (Predicate) 進行的測試,或序列是空的,則為 true
,否則為 false
。
例外狀況
source
或 predicate
為 null
。
範例
下列程式代碼範例示範如何使用 All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 來判斷序列中的所有專案是否符合條件。
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
public static void AllEx1()
{
// 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.AsQueryable().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'.
Sub AllEx()
' Create an array of Pets.
Dim pets() As Pet = _
{New Pet With {.Name = "Barley", .Age = 10}, _
New Pet With {.Name = "Boots", .Age = 4}, _
New Pet With {.Name = "Whiskers", .Age = 6}}
' Determine whether all pet names in the array start with 'B'.
Dim allStartWithB As Boolean = _
pets.AsQueryable().All(Function(ByVal pet) pet.Name.StartsWith("B"))
MsgBox(String.Format( _
"{0} pet names start with 'B'.", _
IIf(allStartWithB, "All", "Not all")))
End Sub
Public Structure Pet
Dim Name As String
Dim Age As Integer
End Structure
' This code produces the following output:
'
' Not all pet names start with 'B'.
方法傳回的布爾值All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)通常用於 Visual Basic 中子句的述詞 (子句) Where
或直接呼叫 Wherewhere
方法。 下列範例示範這個 方法的使用 All
方式。
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.AsQueryable().All(pet => pet.Age > 5)
select person.LastName;
foreach (string name in names)
Console.WriteLine(name);
/* This code produces the following output:
*
* Haas
* Antebi
*/
}
Structure Pet
Public Name As String
Public Age As Integer
End Structure
Structure Person
Public LastName As String
Public Pets() As Pet
End Structure
Sub AllEx2()
Dim people As New List(Of Person)(New Person() _
{New Person With {.LastName = "Haas", _
.Pets = New Pet() {New Pet With {.Name = "Barley", .Age = 10}, _
New Pet With {.Name = "Boots", .Age = 14}, _
New Pet With {.Name = "Whiskers", .Age = 6}}}, _
New Person With {.LastName = "Fakhouri", _
.Pets = New Pet() {New Pet With {.Name = "Snowball", .Age = 1}}}, _
New Person With {.LastName = "Antebi", _
.Pets = New Pet() {New Pet With {.Name = "Belle", .Age = 8}}}, _
New Person With {.LastName = "Philips", _
.Pets = New Pet() {New Pet With {.Name = "Sweetie", .Age = 2}, _
New Pet With {.Name = "Rover", .Age = 13}}}})
' Determine which people have pets that are all older than 5.
Dim names = From person In people _
Where person.Pets.AsQueryable().All(Function(pet) pet.Age > 5) _
Select person.LastName
For Each name As String In names
Console.WriteLine(name)
Next
' This code produces the following output:
'
' Haas
' Antebi
End Sub
備註
這個方法至少有一個類型的參數,其類型 Expression<TDelegate> 自變數為其中一個型別 Func<T,TResult> 。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 Expression<TDelegate>。
方法 All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 會產生 , MethodCallExpression 表示呼叫 All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 本身為建構的泛型方法。 然後,它會將 傳遞給 MethodCallExpressionExecute<TResult>(Expression) 參數之 屬性所Provider表示的方法IQueryProvidersource
。
執行表示呼叫 All<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表達式樹狀結構所產生的查詢行為,取決於參數類型的實 source
作。 預期的行為是它會判斷 中 source
是否符合 條件 predicate
的所有專案。