Queryable.LongCount メソッド

定義

シーケンス内の要素数を表す Int64 を返します。

オーバーロード

LongCount<TSource>(IQueryable<TSource>)

シーケンス内の要素の合計数を表す Int64 を返します。

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

条件を満たす、シーケンス内の要素の数を表す Int64 を返します。

LongCount<TSource>(IQueryable<TSource>)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
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です。

次のコード例では、 を使用 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) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。

呼び出し LongCount<TSource>(IQueryable<TSource>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 内 source の項目の数をカウントし、 を Int64返すということです。

適用対象

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

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
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 の要素数。

例外

source または predicatenull です。

次のコード例では、 を使用 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> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターでは、ラムダ式を渡すことができます。これは に Expression<TDelegate>コンパイルされます。

メソッドは LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>)MethodCallExpression 構築されたジェネリック メソッドとして自身を呼び出すことを LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) 表す を生成します。 次に、 パラメーターの MethodCallExpressionExecute<TResult>(Expression) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。

呼び出し LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 で指定されたpredicate条件を満たす 内sourceの項目の数をカウントし、 をInt64返すということです。

適用対象