Queryable.LongCount Metoda

Definicja

Zwraca element Int64 reprezentujący liczbę elementów w sekwencji.

Przeciążenia

LongCount<TSource>(IQueryable<TSource>)

Zwraca element Int64 reprezentujący całkowitą liczbę elementów w sekwencji.

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

Zwraca element Int64 reprezentujący liczbę elementów w sekwencji spełniającej warunek.

LongCount<TSource>(IQueryable<TSource>)

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca element Int64 reprezentujący całkowitą liczbę elementów w sekwencji.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element IQueryable<T> zawierający elementy do zliczenia.

Zwraca

Liczba elementów w elemecie source.

Wyjątki

source to null.

Liczba elementów przekracza wartość Int64.MaxValue.

Przykłady

W poniższym przykładzie kodu pokazano, jak używać LongCount<TSource>(IQueryable<TSource>) funkcji do zliczenia elementów w tablicy.

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.

Uwagi

Metoda LongCount<TSource>(IQueryable<TSource>) generuje element MethodCallExpression , który reprezentuje wywołanie LongCount<TSource>(IQueryable<TSource>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do Execute<TResult>(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie LongCount<TSource>(IQueryable<TSource>) , zależy od implementacji typu parametru source . Oczekiwane zachowanie polega na tym, że zlicza liczbę elementów i source zwraca wartość Int64.

Dotyczy

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

Źródło:
Queryable.cs
Źródło:
Queryable.cs
Źródło:
Queryable.cs

Zwraca element Int64 reprezentujący liczbę elementów w sekwencji spełniającej warunek.

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

Parametry typu

TSource

Typ elementów elementu source.

Parametry

source
IQueryable<TSource>

Element IQueryable<T> zawierający elementy do zliczenia.

predicate
Expression<Func<TSource,Boolean>>

Funkcja testowania każdego elementu na stanie.

Zwraca

Liczba elementów, które source spełniają warunek w funkcji predykatu.

Wyjątki

source lub predicate to null.

Liczba pasujących elementów przekracza wartość Int64.MaxValue.

Przykłady

W poniższym przykładzie kodu pokazano, jak użyć LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) funkcji do zliczenia elementów w tablicy spełniającej warunek.

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.

Uwagi

Ta metoda ma co najmniej jeden parametr typu Expression<TDelegate> , którego argument type jest jednym z Func<T,TResult> typów. W przypadku tych parametrów można przekazać wyrażenie lambda i zostanie skompilowane do elementu Expression<TDelegate>.

Metoda LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) generuje element MethodCallExpression , który reprezentuje wywołanie LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) siebie jako skonstruowaną metodę ogólną. Następnie przekazuje MethodCallExpression element do Execute<TResult>(Expression) metody reprezentowanej IQueryProvider przez Provider właściwość parametru source .

Zachowanie zapytania, które występuje w wyniku wykonania drzewa wyrażeń, które reprezentuje wywołanie LongCount<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) , zależy od implementacji typu parametru source . Oczekiwanym zachowaniem jest to, że zlicza liczbę elementów, które source spełniają warunek określony przez predicate i zwraca wartość Int64.

Dotyczy