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 static long LongCount<TSource> (this System.Linq.IQueryable<TSource> source);

类型参数

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.
*/

注解

方法 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 static long LongCount<TSource> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,bool>> predicate);

类型参数

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.
*/

注解

此方法至少有一个类型 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

适用于