Enumerable.LongCount Método
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Retorna e Int64 que representa o número de elementos numa sequência.
Sobrecargas
| Name | Description |
|---|---|
| LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
Devolve e Int64 que representa quantos elementos numa sequência satisfazem uma condição. |
| LongCount<TSource>(IEnumerable<TSource>) |
Retorna e Int64 que representa o número total de elementos numa sequência. |
LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
Devolve e Int64 que representa quantos elementos numa sequência satisfazem uma condição.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static long LongCount(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, bool> ^ predicate);
public static long LongCount<TSource>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);
static member LongCount : seq<'Source> * Func<'Source, bool> -> int64
<Extension()>
Public Function LongCount(Of TSource) (source As IEnumerable(Of TSource), predicate As Func(Of TSource, Boolean)) As Long
Parâmetros de Tipo Genérico
- TSource
O tipo dos elementos de source.
Parâmetros
- source
- IEnumerable<TSource>
E IEnumerable<T> que contém os elementos a contar.
Devoluções
Um número que representa quantos elementos na sequência satisfazem a condição na função de predicado.
Exceções
source ou predicate é null.
O número de elementos correspondentes excede o Int64.MaxValue.
Exemplos
O seguinte exemplo de código demonstra como usar LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) a contagem dos elementos num array que satisfazem uma condição.
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;
long count = pets.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
Sub LongCountEx2()
' Create a list of Pet objects.
Dim pets As New List(Of Pet)(New Pet() _
{New Pet With {.Name = "Barley", .Age = 8},
New Pet With {.Name = "Boots", .Age = 4},
New Pet With {.Name = "Whiskers", .Age = 1}})
' Determine the number of elements in the list
' where the pet's age is greater than a constant value (3).
Const Age As Integer = 3
Dim count As Long =
pets.LongCount(Function(pet) pet.Age > Age)
' Display the result.
Console.WriteLine($"There are {count} animals over age {Age}")
End Sub
' This code produces the following output:
'
' There are 2 animals over age 3
Observações
Use este método em vez de Count esperar que o resultado seja superior a MaxValue.
Na sintaxe Visual Basic expressão de consulta, uma cláusula Aggregate Into LongCount() traduz-se numa invocação de LongCount.
Ver também
Agregada (Visual Basic)
Aplica-se a
LongCount<TSource>(IEnumerable<TSource>)
Retorna e Int64 que representa o número total de elementos numa sequência.
public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
static long LongCount(System::Collections::Generic::IEnumerable<TSource> ^ source);
public static long LongCount<TSource>(this System.Collections.Generic.IEnumerable<TSource> source);
static member LongCount : seq<'Source> -> int64
<Extension()>
Public Function LongCount(Of TSource) (source As IEnumerable(Of TSource)) As Long
Parâmetros de Tipo Genérico
- TSource
O tipo dos elementos de source.
Parâmetros
- source
- IEnumerable<TSource>
E IEnumerable<T> que contém os elementos a contar.
Devoluções
O número de elementos na sequência de origem.
Exceções
source é null.
O número de elementos excede o Int64.MaxValue.
Exemplos
O exemplo de código seguinte demonstra como usar LongCount<TSource>(IEnumerable<TSource>) a contagem dos elementos num array.
string[] fruits = { "apple", "banana", "mango",
"orange", "passionfruit", "grape" };
long count = fruits.LongCount();
Console.WriteLine("There are {0} fruits in the collection.", count);
/*
This code produces the following output:
There are 6 fruits in the collection.
*/
' Create an array of strings.
Dim fruits() As String =
{"apple", "banana", "mango", "orange", "passionfruit", "grape"}
' Get the number of items in the array.
Dim count As Long = fruits.LongCount()
' Display the result.
Console.WriteLine($"There are {count} fruits in the collection.")
' This code produces the following output:
'
' There are 6 fruits in the collection.
Observações
Use este método em vez de Count esperar que o resultado seja superior a MaxValue.
Na sintaxe Visual Basic expressão de consulta, uma cláusula Aggregate Into LongCount() traduz-se numa invocação de LongCount.
Ver também
Agregada (Visual Basic)