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

C#
public static long LongCount<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

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

傳回

來源序列中的項目數目。

例外狀況

sourcenull

元素數目超過 Int64.MaxValue

範例

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

C#
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.
*/

備註

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

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

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

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

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

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

C#
public static long LongCount<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

類型參數

TSource

source 項目的類型。

參數

source
IEnumerable<TSource>

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

predicate
Func<TSource,Boolean>

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

傳回

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

例外狀況

sourcepredicatenull

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

範例

下列程式代碼範例示範如何使用 LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) 來計算滿足條件之陣列中的元素。

C#
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.
*/

備註

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

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

另請參閱

適用於

.NET 9 和其他版本
產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0