Queryable.LongCount 方法

定义

返回一个 Int64,表示序列中的元素的数量。

重载

LongCount<TSource>(IQueryable<TSource>)

返回表示序列中元素总数的 Int64

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

返回一个 Int64,它表示序列中满足条件的元素数量。

LongCount<TSource>(IQueryable<TSource>)

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

返回表示序列中元素总数的 Int64

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static long LongCount(System::Linq::IQueryable<TSource> ^ source);
public static long LongCount<TSource> (this System.Linq.IQueryable<TSource> source);
static member LongCount : System.Linq.IQueryable<'Source> -> int64
<Extension()>
Public Function LongCount(Of TSource) (source As IQueryable(Of TSource)) As Long

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

包含要进行计数的元素的 IQueryable<T>

返回

source 中的元素数。

例外

sourcenull

元素数超过 Int64.MaxValue

示例

下面的代码示例演示如何使用 LongCount<TSource>(IQueryable<TSource>) 对数组中的元素进行计数。

string[] fruits = { "apple", "banana", "mango",
                      "orange", "passionfruit", "grape" };

long count = fruits.AsQueryable().LongCount();

Console.WriteLine("There are {0} fruits in the collection.", count);

/*
    This code produces the following output:

    There are 6 fruits in the collection.
*/
Dim fruits() As String = {"apple", "banana", "mango", _
                      "orange", "passionfruit", "grape"}

Dim count As Long = fruits.AsQueryable().LongCount()

MsgBox(String.Format("There are {0} fruits in the collection.", count))

' This code produces the following output:

' There are 6 fruits in the collection.

注解

方法 LongCount<TSource>(IQueryable<TSource>) 生成一个 , MethodCallExpression 表示将调用 LongCount<TSource>(IQueryable<TSource>) 自身作为构造的泛型方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

由于执行表示调用 LongCount<TSource>(IQueryable<TSource>) 的表达式树而发生的查询行为取决于参数类型的 source 实现。 预期的行为是,它会对 中的 source 项数进行计数,并返回 Int64

适用于

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

Source:
Queryable.cs
Source:
Queryable.cs
Source:
Queryable.cs

返回一个 Int64,它表示序列中满足条件的元素数量。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static long LongCount(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, bool> ^> ^ predicate);
public static long LongCount<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);
static member LongCount : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, bool>> -> int64
<Extension()>
Public Function LongCount(Of TSource) (source As IQueryable(Of TSource), predicate As Expression(Of Func(Of TSource, Boolean))) As Long

类型参数

TSource

source 的元素类型。

参数

source
IQueryable<TSource>

包含要进行计数的元素的 IQueryable<T>

predicate
Expression<Func<TSource,Boolean>>

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

返回

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

例外

sourcepredicatenull

匹配的元素数超过 Int64.MaxValue

示例

下面的代码示例演示如何使用 LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 对满足条件的数组中的元素进行计数。

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void LongCountEx2()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    const int Age = 3;

    // Count the number of Pet objects where Pet.Age is greater than 3.
    long count = pets.AsQueryable().LongCount(pet => pet.Age > Age);

    Console.WriteLine("There are {0} animals over age {1}.", count, Age);
}

/*
    This code produces the following output:

    There are 2 animals over age 3.
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Shared Sub LongCountEx2()
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8}, _
                       New Pet With {.Name = "Boots", .Age = 4}, _
                       New Pet With {.Name = "Whiskers", .Age = 1}}

    Const Age As Integer = 3

    ' Count the number of Pet objects where Pet.Age is greater than 3.
    Dim count As Long = pets.AsQueryable().LongCount(Function(Pet) Pet.Age > Age)

    MsgBox(String.Format("There are {0} animals over age {1}.", count, Age))
End Sub

' This code produces the following output:

' There are 2 animals over age 3.

注解

此方法至少有一个类型的 Expression<TDelegate> 参数,其类型参数是其中一种 Func<T,TResult> 类型。 对于这些参数,可以传入 lambda 表达式,它将编译为 Expression<TDelegate>

方法 LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 生成一个 , MethodCallExpression 表示将调用 LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 自身作为构造的泛型方法。 然后,MethodCallExpressionExecute<TResult>(Expression)它将 传递给 由 Provider 参数的 属性表示的 的 source 方法IQueryProvider

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

适用于