Queryable.Count Metodo

Definizione

Restituisce il numero di elementi in una sequenza.

Overload

Count<TSource>(IQueryable<TSource>)

Restituisce il numero di elementi in una sequenza.

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

Restituisce il numero di elementi nella sequenza specificata che soddisfano una condizione.

Count<TSource>(IQueryable<TSource>)

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

Restituisce il numero di elementi in una sequenza.

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

Parametri di tipo

TSource

Tipo degli elementi di source.

Parametri

source
IQueryable<TSource>

Oggetto IQueryable<T> che contiene gli elementi da contare.

Restituisce

Numero di elementi nella sequenza di input.

Eccezioni

source è null.

Il numero di elementi in source è maggiore di Int32.MaxValue.

Esempio

Nell'esempio di codice seguente viene illustrato come usare Count<TSource>(IQueryable<TSource>) per contare gli elementi in una sequenza.

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.

Commenti

Il Count<TSource>(IQueryable<TSource>) metodo genera un MethodCallExpression oggetto che rappresenta Count<TSource>(IQueryable<TSource>) se stesso come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al Execute<TResult>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro .

Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata Count<TSource>(IQueryable<TSource>) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che conta il numero di elementi in source.

Si applica a

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

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

Restituisce il numero di elementi nella sequenza specificata che soddisfano una condizione.

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

Parametri di tipo

TSource

Tipo degli elementi di source.

Parametri

source
IQueryable<TSource>

Oggetto IQueryable<T> che contiene gli elementi da contare.

predicate
Expression<Func<TSource,Boolean>>

Funzione per testare ogni elemento rispetto a una condizione.

Restituisce

Il numero di elementi nella sequenza che soddisfa la condizione nella funzione predicativa.

Eccezioni

source o predicate è null.

Il numero di elementi in source è maggiore di Int32.MaxValue.

Esempio

Nell'esempio di codice seguente viene illustrato come utilizzare Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) per contare gli elementi in una sequenza che soddisfano una condizione.

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.

Commenti

Questo metodo ha almeno un parametro di tipo Expression<TDelegate> il cui argomento di tipo è uno dei Func<T,TResult> tipi . Per questi parametri, è possibile passare un'espressione lambda e verrà compilata in un oggetto Expression<TDelegate>.

Il Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) metodo genera un MethodCallExpression oggetto che rappresenta Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) se stesso come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al Execute<TResult>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro .

Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata Count<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che conta il numero di elementi in source che soddisfano la condizione specificata da predicate.

Si applica a