Queryable.Count 方法

定义

返回序列中的元素数量。

重载

Count<TSource>(IQueryable<TSource>)

返回序列中的元素数量。

Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

返回指定序列中满足条件的元素数量。

Count<TSource>(IQueryable<TSource>)

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);
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>

返回

输入序列中的元素数量。

例外

sourcenull

中的 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>) 自身表示为构造的泛型方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 Count<TSource>(IQueryable<TSource>) 的表达式树而发生的查询行为取决于参数类型的 source 实现。 预期行为是计算 中的 source项数。

适用于

Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)

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);
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>>

用于测试每个元素是否满足条件的函数。

返回

序列中满足谓词函数的条件的元素数量。

例外

sourcepredicatenull

中的 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 == false);

    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>>) 自身表示为构造的泛型方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 的表达式树而发生的查询行为取决于参数类型的 source 实现。 预期行为是,它计算 中 source 满足 所 predicate指定条件的项数。

适用于