Queryable.Count 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回序列中的項目數目。
多載
| 名稱 | Description |
|---|---|
| Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) |
傳回符合條件之指定序列中的項目數目。 |
| Count<TSource>(IQueryable<TSource>) |
傳回序列中的項目數目。 |
Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
傳回符合條件之指定序列中的項目數目。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static int Count(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static int Count<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static int Count<TSource>(this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member Count : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> int
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member Count : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> int
<Extension()>
Public Function Count(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As Integer
類型參數
- TSource
元素 source的類型。
參數
- source
- IQueryable<TSource>
一個 IQueryable<T> 包含待計數元素的 。
- predicate
- Expression<Func<TSource,Boolean>>
一個用來測試每個元素條件的函數。
傳回
滿足謂詞函數條件的序列元素數量。
- 屬性
例外狀況
source 或 predicate 為 null。
其中 source 的元素數量比 Int32.MaxValue 還多。
範例
以下程式碼範例示範如何用 Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 來計算符合條件的序列中元素。
class Pet
{
public string Name { get; set; }
public bool Vaccinated { get; set; }
}
public static void CountEx2()
{
// Create an array of Pet objects.
Pet[] pets = { new Pet { Name="Barley", Vaccinated=true },
new Pet { Name="Boots", Vaccinated=false },
new Pet { Name="Whiskers", Vaccinated=false } };
// Count the number of unvaccinated pets in the array.
int numberUnvaccinated =
pets.AsQueryable().Count(p => !p.Vaccinated);
Console.WriteLine(
"There are {0} unvaccinated animals.",
numberUnvaccinated);
}
// This code produces the following output:
//
// There are 2 unvaccinated animals.
Structure Pet
Public Name As String
Public Vaccinated As Boolean
End Structure
Shared Sub CountEx2()
' Create an array of Pet objects.
Dim pets() As Pet = {New Pet With {.Name = "Barley", .Vaccinated = True}, _
New Pet With {.Name = "Boots", .Vaccinated = False}, _
New Pet With {.Name = "Whiskers", .Vaccinated = False}}
' Count the number of unvaccinated pets in the array.
Dim numberUnvaccinated As Integer = pets.AsQueryable().Count(Function(p) p.Vaccinated = False)
MsgBox(String.Format("There are {0} unvaccinated animals.", numberUnvaccinated))
End Sub
' This code produces the following output:
'
' There are 2 unvaccinated animals.
備註
此方法至少有一個型別 Expression<TDelegate> 參數,其型別參數的參數是其中一個 Func<T,TResult> 型別。 對於這些參數,你可以輸入 lambda 運算式,然後它會被編譯成 Expression<TDelegate>。
該 Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 方法產生的 是 MethodCallExpression ,代表 Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 呼叫本身為一個構造化的泛型方法。 接著將 傳遞MethodCallExpression給Execute<TResult>(Expression)IQueryProvider由參數Provider性質source所表示的方法。
執行代表呼叫 Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期行為是計算中滿足條件predicate的項目source數量。
適用於
Count<TSource>(IQueryable<TSource>)
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
- 來源:
- Queryable.cs
傳回序列中的項目數目。
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static int Count(System::Linq::IQueryable<TSource> ^ source);
public static int Count<TSource>(this System.Linq.IQueryable<TSource> source);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static int Count<TSource>(this System.Linq.IQueryable<TSource> source);
static member Count : System.Linq.IQueryable<'Source> -> int
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member Count : System.Linq.IQueryable<'Source> -> int
<Extension()>
Public Function Count(Of TSource) (source As IQueryable(Of TSource)) As Integer
類型參數
- TSource
元素 source的類型。
參數
- source
- IQueryable<TSource>
該 IQueryable<T> 包含了要計算的元素。
傳回
輸入序列中的元素數量。
- 屬性
例外狀況
source 為 null。
其中 source 的元素數量比 Int32.MaxValue 還多。
範例
以下程式碼範例示範如何使用來 Count<TSource>(IQueryable<TSource>) 計算序列中的元素。
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
int numberOfFruits = fruits.AsQueryable().Count();
Console.WriteLine(
"There are {0} items in the array.",
numberOfFruits);
// This code produces the following output:
//
// There are 6 items in the array.
Dim fruits() As String = {"apple", "banana", "mango", _
"orange", "passionfruit", "grape"}
Dim numberOfFruits As Integer = fruits.AsQueryable().Count()
MsgBox(String.Format( _
"There are {0} items in the array.", _
numberOfFruits))
' This code produces the following output:
'
' There are 6 items in the array.
備註
該 Count<TSource>(IQueryable<TSource>) 方法產生的 是 MethodCallExpression ,代表 Count<TSource>(IQueryable<TSource>) 呼叫本身為一個構造化的泛型方法。 接著將 傳遞MethodCallExpression給Execute<TResult>(Expression)IQueryProvider由參數Provider性質source所表示的方法。
執行代表呼叫 Count<TSource>(IQueryable<TSource>) 的表達式樹所產生的查詢行為,取決於參數型別 source 的實作。 預期行為是它會計算 中的 source項目數量。