共用方式為


Enumerable.LongCount 方法

定義

傳回代表序列中項目數目的 Int64

多載

LongCount<TSource>(IEnumerable<TSource>)

傳回代表序列中項目總數的 Int64

LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

傳回 Int64,其代表序列中符合條件的項目數目。

LongCount<TSource>(IEnumerable<TSource>)

來源:
Count.cs
來源:
Count.cs
來源:
Count.cs

傳回代表序列中項目總數的 Int64

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

包含要計算之項目的 IEnumerable<T>

傳回

來源序列中的項目數目。

例外狀況

sourcenull

元素數目超過 Int64.MaxValue

範例

下列程式代碼範例示範如何使用 LongCount<TSource>(IEnumerable<TSource>) 來計算數位列中的專案。

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.

備註

當您預期結果大於 MaxValue時,請使用這個方法,而不是 Count

在 Visual Basic 查詢表達式語法中, Aggregate Into LongCount() 子句會轉譯為的 LongCount調用。

另請參閱

適用於

LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)

來源:
Count.cs
來源:
Count.cs
來源:
Count.cs

傳回 Int64,其代表序列中符合條件的項目數目。

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

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

包含要計算之項目的 IEnumerable<T>

predicate
Func<TSource,Boolean>

用來測試每個項目是否符合條件的函式。

傳回

數字,代表序列中符合述詞函式之條件的項目數目。

例外狀況

sourcepredicatenull

相符項目的數目超過 Int64.MaxValue

範例

下列程式代碼範例示範如何使用 LongCount<TSource>(IEnumerable<TSource>, 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;

    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

備註

當您預期結果大於 MaxValue時,請使用這個方法,而不是 Count

在 Visual Basic 查詢表達式語法中, Aggregate Into LongCount() 子句會轉譯為的 LongCount調用。

另請參閱

適用於