Queryable.Count 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回序列中的元素数。
重载
| 名称 | 说明 |
|---|---|
| Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) |
返回满足条件的指定序列中的元素数。 |
| Count<TSource>(IQueryable<TSource>) |
返回序列中的元素数。 |
Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- 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>>)生成一个表示调用Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)自身为已构造泛型方法的一MethodCallExpression个方法。 然后,它将传递给MethodCallExpressionExecute<TResult>(Expression)由参数属性source表示Provider的方法IQueryProvider。
执行表示调用 Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它计算满足指定predicate条件的项source数。
适用于
Count<TSource>(IQueryable<TSource>)
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- Queryable.cs
- Source:
- 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>)生成一个表示调用Count<TSource>(IQueryable<TSource>)自身为已构造泛型方法的一MethodCallExpression个方法。 然后,它将传递给MethodCallExpressionExecute<TResult>(Expression)由参数属性source表示Provider的方法IQueryProvider。
执行表示调用 Count<TSource>(IQueryable<TSource>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是它计算项 source的数目。