Enumerable.LongCount 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳 和 Int64 ,代表序列中的元素數量。
多載
| 名稱 | Description |
|---|---|
| LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>) |
傳回 Int64,代表序列中滿足條件的項目數目。 |
| LongCount<TSource>(IEnumerable<TSource>) |
傳回代表序列中項目總數的 Int64。 |
LongCount<TSource>(IEnumerable<TSource>, Func<TSource,Boolean>)
- 來源:
- Count.cs
- 來源:
- Count.cs
- 來源:
- 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> 包含待計數元素的 。
傳回
一個數字,代表序列中有多少元素滿足謂詞函數的條件。
例外狀況
source 或 predicate 為 null。
匹配元素數量超過 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
備註
使用此方法,而非 Count 預期 MaxValue結果大於 時才使用。
在Visual Basic查詢表達式語法中,Aggregate Into LongCount()子句可轉譯為LongCount的呼叫。
另請參閱
適用於
LongCount<TSource>(IEnumerable<TSource>)
- 來源:
- Count.cs
- 來源:
- Count.cs
- 來源:
- 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> 包含待計數元素的 。
傳回
來源序列中的元素數量。
例外狀況
source 是 null。
元素數超過 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.
備註
使用此方法,而非 Count 預期 MaxValue結果大於 時才使用。
在Visual Basic查詢表達式語法中,Aggregate Into LongCount()子句可轉譯為LongCount的呼叫。