Queryable.Count Metoda

Definicja

Zwraca liczbę elementów w sekwencji.

Przeciążenia

Count<TSource>(IQueryable<TSource>)

Zwraca liczbę elementów w sekwencji.

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

Zwraca liczbę elementów w określonej sekwencji, która spełnia warunek.

Count<TSource>(IQueryable<TSource>)

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

Zwraca liczbę elementów w sekwencji.

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

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 sekwencji danych wejściowych.

Wyjątki

source to null.

Liczba elementów w elemecie source jest większa niż Int32.MaxValue.

Przykłady

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

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.

Uwagi

Metoda Count<TSource>(IQueryable<TSource>) generuje element MethodCallExpression , który reprezentuje wywołanie Count<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 Count<TSource>(IQueryable<TSource>) , zależy od implementacji typu parametru source . Oczekiwanym zachowaniem jest to, że zlicza liczbę elementów w sourceelemecie .

Dotyczy

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

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

Zwraca liczbę elementów w określonej sekwencji, która spełnia warunek.

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

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 w sekwencji, która spełnia warunek w funkcji predykatu.

Wyjątki

source lub predicate to null.

Liczba elementów w elemecie source jest większa niż Int32.MaxValue.

Przykłady

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

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.

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 Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) generuje element MethodCallExpression , który reprezentuje wywołanie Count<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 Count<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.

Dotyczy