Enumerable.GroupBy 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
群組序列的項目。
多載
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
依據指定的索引鍵選取器函式來群組序列的項目,並從每個群組及其索引鍵建立結果值。 每個群組的項目都是利用指定的函式進行投影。
public:
generic <typename TSource, typename TKey, typename TElement, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TSource, TElement> ^ elementSelector, Func<TKey, System::Collections::Generic::IEnumerable<TElement> ^, TResult> ^ resultSelector);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TElement,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> * Func<'Key, seq<'Element>, 'Result> -> seq<'Result>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TElement, TResult) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), elementSelector As Func(Of TSource, TElement), resultSelector As Func(Of TKey, IEnumerable(Of TElement), TResult)) As IEnumerable(Of TResult)
類型參數
- TSource
source
項目的類型。
- TKey
keySelector
所傳回之索引鍵的型別。
- TElement
每個 IGrouping<TKey,TElement> 中的項目型別。
- TResult
resultSelector
所傳回之結果值的型別。
參數
- source
- IEnumerable<TSource>
要群組其項目的 IEnumerable<T>。
- keySelector
- Func<TSource,TKey>
用來擷取各項目之索引鍵的函式。
- elementSelector
- Func<TSource,TElement>
用來將每個來源項目對應至 IGrouping<TKey,TElement> 之項目的函式。
- resultSelector
- Func<TKey,IEnumerable<TElement>,TResult>
用來從各個群組建立結果值的函式。
傳回
TResult
型別項目的集合,其中每個項目都代表群組及其索引鍵的投影。
例外狀況
source
、keySelector
、elementSelector
或 resultSelector
為 null
。
範例
下列程式碼範例示範如何使用 GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>) 將序列的投影專案分組,然後投影 類型 TResult
的結果序列。
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
public static void GroupByEx4()
{
// Create a list of pets.
List<Pet> petsList =
new List<Pet>{ new Pet { Name="Barley", Age=8.3 },
new Pet { Name="Boots", Age=4.9 },
new Pet { Name="Whiskers", Age=1.5 },
new Pet { Name="Daisy", Age=4.3 } };
// Group Pet.Age values by the Math.Floor of the age.
// Then project an anonymous type from each group
// that consists of the key, the count of the group's
// elements, and the minimum and maximum age in the group.
var query = petsList.GroupBy(
pet => Math.Floor(pet.Age),
pet => pet.Age,
(baseAge, ages) => new
{
Key = baseAge,
Count = ages.Count(),
Min = ages.Min(),
Max = ages.Max()
});
// Iterate over each anonymous type.
foreach (var result in query)
{
Console.WriteLine("\nAge group: " + result.Key);
Console.WriteLine("Number of pets in this age group: " + result.Count);
Console.WriteLine("Minimum age: " + result.Min);
Console.WriteLine("Maximum age: " + result.Max);
}
/* This code produces the following output:
Age group: 8
Number of pets in this age group: 1
Minimum age: 8.3
Maximum age: 8.3
Age group: 4
Number of pets in this age group: 2
Minimum age: 4.3
Maximum age: 4.9
Age group: 1
Number of pets in this age group: 1
Minimum age: 1.5
Maximum age: 1.5
*/
}
Structure Pet
Public Name As String
Public Age As Double
End Structure
Public Sub GroupByEx4()
' Create a list of pets.
Dim petsList As New List(Of Pet)(New Pet() _
{New Pet With {.Name = "Barley", .Age = 8.3},
New Pet With {.Name = "Boots", .Age = 4.9},
New Pet With {.Name = "Whiskers", .Age = 1.5},
New Pet With {.Name = "Daisy", .Age = 4.3}})
' Group Pet.Age values by the Math.Floor of the age.
' Then project an anonymous type from each group
' that consists of the key, the count of the group's
' elements, and the minimum and maximum age in the group.
Dim query = petsList.GroupBy(
Function(pet) Math.Floor(pet.Age),
Function(pet) pet.Age,
Function(baseAge, ages) New With
{.Key = baseAge,
.Count = ages.Count(),
.Min = ages.Min(),
.Max = ages.Max()}
)
Dim output As New System.Text.StringBuilder
' Iterate over each anonymous type.
For Each result In query
output.AppendLine(vbCrLf & "Age group: " & result.Key)
output.AppendLine("Number of pets in this age group: " & result.Count)
output.AppendLine("Minimum age: " & result.Min)
output.AppendLine("Maximum age: " & result.Max)
Next
' Display the output.
Console.WriteLine(output.ToString)
End Sub
' This code produces the following output:
' Age group: 8
' Number of pets in this age group: 1
' Minimum age: 8.3
' Maximum age: 8.3
'
' Age group: 4
' Number of pets in this age group: 2
' Minimum age: 4.3
' Maximum age: 4.9
'
' Age group: 1
' Number of pets in this age group: 1
' Minimum age: 1.5
' Maximum age: 1.5
備註
在查詢運算式語法中, group by
(C#) 或 Group By Into
(Visual Basic) 子句會轉譯為 的 GroupBy 調用。
另請參閱
適用於
GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>, TResult>, IEqualityComparer<TKey>)
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
依據指定的索引鍵選取器函式來群組序列的項目,並從每個群組及其索引鍵建立結果值。 索引鍵值是使用指定的比較子來進行比較,而每個群組的項目則都是利用指定的函式進行投影。
public:
generic <typename TSource, typename TKey, typename TElement, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TSource, TElement> ^ elementSelector, Func<TKey, System::Collections::Generic::IEnumerable<TElement> ^, TResult> ^ resultSelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TElement,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TElement,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, Func<TKey,System.Collections.Generic.IEnumerable<TElement>,TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> * Func<'Key, seq<'Element>, 'Result> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<'Result>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TElement, TResult) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), elementSelector As Func(Of TSource, TElement), resultSelector As Func(Of TKey, IEnumerable(Of TElement), TResult), comparer As IEqualityComparer(Of TKey)) As IEnumerable(Of TResult)
類型參數
- TSource
source
項目的類型。
- TKey
keySelector
所傳回之索引鍵的型別。
- TElement
每個 IGrouping<TKey,TElement> 中的項目型別。
- TResult
resultSelector
所傳回之結果值的型別。
參數
- source
- IEnumerable<TSource>
要群組其項目的 IEnumerable<T>。
- keySelector
- Func<TSource,TKey>
用來擷取各項目之索引鍵的函式。
- elementSelector
- Func<TSource,TElement>
用來將每個來源項目對應至 IGrouping<TKey,TElement> 之項目的函式。
- resultSelector
- Func<TKey,IEnumerable<TElement>,TResult>
用來從各個群組建立結果值的函式。
- comparer
- IEqualityComparer<TKey>
用來比較索引鍵的 IEqualityComparer<T>。
傳回
TResult
型別項目的集合,其中每個項目都代表群組及其索引鍵的投影。
例外狀況
source
、keySelector
、elementSelector
或 resultSelector
為 null
。
另請參閱
適用於
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
依據指定的索引鍵選取器函式來群組序列的項目,並使用指定的函式來投影每個群組的項目。
public:
generic <typename TSource, typename TKey, typename TElement>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Linq::IGrouping<TKey, TElement> ^> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TSource, TElement> ^ elementSelector);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> -> seq<System.Linq.IGrouping<'Key, 'Element>>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TElement) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), elementSelector As Func(Of TSource, TElement)) As IEnumerable(Of IGrouping(Of TKey, TElement))
類型參數
- TSource
source
項目的類型。
- TKey
keySelector
所傳回之索引鍵的型別。
- TElement
IGrouping<TKey,TElement> 中的項目類型。
參數
- source
- IEnumerable<TSource>
要群組其項目的 IEnumerable<T>。
- keySelector
- Func<TSource,TKey>
用來擷取各項目之索引鍵的函式。
- elementSelector
- Func<TSource,TElement>
用來將每個來源項目對應至 IGrouping<TKey,TElement> 之項目的函式。
傳回
C# 或 IEnumerable(Of IGrouping(Of TKey, TElement))
Visual Basic 中的 , IEnumerable<IGrouping<TKey, TElement>>
其中每個 IGrouping<TKey,TElement> 物件都包含型 TElement
別和索引鍵的物件集合。
例外狀況
source
、keySelector
或 elementSelector
為 null
。
範例
下列程式碼範例示範如何使用 GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) 來群組序列的專案。
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
// Uses method-based query syntax.
public static void GroupByEx1()
{
// Create a list of pets.
List<Pet> pets =
new List<Pet>{ new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 },
new Pet { Name="Daisy", Age=4 } };
// Group the pets using Age as the key value
// and selecting only the pet's Name for each value.
IEnumerable<IGrouping<int, string>> query =
pets.GroupBy(pet => pet.Age, pet => pet.Name);
// Iterate over each IGrouping in the collection.
foreach (IGrouping<int, string> petGroup in query)
{
// Print the key value of the IGrouping.
Console.WriteLine(petGroup.Key);
// Iterate over each value in the
// IGrouping and print the value.
foreach (string name in petGroup)
Console.WriteLine(" {0}", name);
}
}
/*
This code produces the following output:
8
Barley
4
Boots
Daisy
1
Whiskers
*/
Structure Pet
Public Name As String
Public Age As Integer
End Structure
Sub GroupByEx1()
'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},
New Pet With {.Name = "Daisy", .Age = 4}})
' Group the pets using Age as the key
' and selecting only the pet's Name for each value.
Dim query As IEnumerable(Of IGrouping(Of Integer, String)) =
pets.GroupBy(Function(pet) pet.Age,
Function(pet) pet.Name)
Dim output As New System.Text.StringBuilder
' Iterate over each IGrouping in the collection.
For Each petGroup As IGrouping(Of Integer, String) In query
' Print the key value of the IGrouping.
output.AppendLine(petGroup.Key)
' Iterate over each value in the IGrouping and print the value.
For Each name As String In petGroup
output.AppendLine(" " & name)
Next
Next
' Display the output.
Console.WriteLine(output.ToString)
End Sub
' This code produces the following output:
'
' 8
' Barley
' 4
' Boots
' Daisy
' 1
' Whiskers
在查詢運算式語法中, group by
(C#) 或 Group By Into
(Visual Basic) 子句會轉譯為 的 GroupBy 調用。 下列範例中查詢運算式的轉譯相當於上述範例中的查詢。
IEnumerable<IGrouping<int, string>> query =
from pet in pets
group pet.Name by pet.Age;
Dim query =
From pet In pets
Group pet.Name By Age = pet.Age Into ageGroup = Group
注意
在 C# 或 Visual Basic 查詢運算式中,元素和索引鍵選取運算式會以與方法呼叫 GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) 中的引數位置相反順序發生。
備註
這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 GetEnumerator
方法或在 C# 或 foreach
For Each
Visual Basic 中使用 來列舉物件,否則不會執行這個方法所代表的查詢。
方法 GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) 會傳回 物件的集合 IGrouping<TKey,TElement> ,每個遇到的不同索引鍵各有一個。 IGrouping<TKey,TElement>是 IEnumerable<T> ,其也有與其專案相關聯的索引鍵。
根據 IGrouping<TKey,TElement> 中產生每個 的第一個索引鍵的專案 source
順序,物件會依順序產生。 IGrouping<TKey,TElement> 群組中的專案會依產生產生這些專案的順序出現在 中 source
。
預設相等比較子 Default 是用來比較索引鍵。
另請參閱
適用於
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
依據索引鍵選取器函式來群組序列中的項目。 索引鍵是使用比較子來進行比較,而每個群組的項目都是利用指定的函式進行投影。
public:
generic <typename TSource, typename TKey, typename TElement>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Linq::IGrouping<TKey, TElement> ^> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TSource, TElement> ^ elementSelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TElement>> GroupBy<TSource,TKey,TElement> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TSource,TElement> elementSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Source, 'Element> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<System.Linq.IGrouping<'Key, 'Element>>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TElement) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), elementSelector As Func(Of TSource, TElement), comparer As IEqualityComparer(Of TKey)) As IEnumerable(Of IGrouping(Of TKey, TElement))
類型參數
- TSource
source
項目的類型。
- TKey
keySelector
所傳回之索引鍵的型別。
- TElement
IGrouping<TKey,TElement> 中的項目類型。
參數
- source
- IEnumerable<TSource>
要群組其項目的 IEnumerable<T>。
- keySelector
- Func<TSource,TKey>
用來擷取各項目之索引鍵的函式。
- elementSelector
- Func<TSource,TElement>
用來將每個來源項目對應至 IGrouping<TKey,TElement> 之項目的函式。
- comparer
- IEqualityComparer<TKey>
用來比較金鑰的 IEqualityComparer<T>。
傳回
C# 或 IEnumerable(Of IGrouping(Of TKey, TElement))
Visual Basic 中的 , IEnumerable<IGrouping<TKey, TElement>>
其中每個 IGrouping<TKey,TElement> 物件都包含 型 TElement
別和索引鍵的物件集合。
例外狀況
source
、keySelector
或 elementSelector
為 null
。
備註
這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 GetEnumerator
方法或在 C# 或 foreach
For Each
Visual Basic 中使用 來列舉物件,否則不會執行這個方法所代表的查詢。
注意
如需 的 GroupBy
範例,請參閱下列文章:
- GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
- GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)
- GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
方法 GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>) 會傳回 物件的集合 IGrouping<TKey,TElement> ,每個遇到的不同索引鍵各有一個。 IGrouping<TKey,TElement>是 IEnumerable<T> ,其也有與其專案相關聯的索引鍵。
根據 IGrouping<TKey,TElement> 中產生每個 的第一個索引鍵的專案 source
順序,物件會依順序產生。 IGrouping<TKey,TElement> 群組中的專案會依產生產生這些專案的順序出現在 中 source
。
如果 為 comparer
null
,則會使用預設相等比較子 Default 來比較索引鍵。
如果兩個索引鍵視為相等, comparer
則會選擇第一個索引鍵作為該群組的索引鍵。
在查詢運算式語法中, group by
(C#) 或 Group By Into
(Visual Basic) 子句會轉譯為 的 GroupBy 調用。 如需詳細資訊和使用範例,請參閱 group 子句 和 Group By 子句。
另請參閱
適用於
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
依據指定的索引鍵選取器函式來群組序列的項目,並從每個群組及其索引鍵建立結果值。
public:
generic <typename TSource, typename TKey, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TKey, System::Collections::Generic::IEnumerable<TSource> ^, TResult> ^ resultSelector);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult> resultSelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Key, seq<'Source>, 'Result> -> seq<'Result>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TResult) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), resultSelector As Func(Of TKey, IEnumerable(Of TSource), TResult)) As IEnumerable(Of TResult)
類型參數
- TSource
source
項目的類型。
- TKey
keySelector
所傳回之索引鍵的型別。
- TResult
resultSelector
所傳回之結果值的型別。
參數
- source
- IEnumerable<TSource>
要群組其項目的 IEnumerable<T>。
- keySelector
- Func<TSource,TKey>
用來擷取各項目之索引鍵的函式。
- resultSelector
- Func<TKey,IEnumerable<TSource>,TResult>
用來從各個群組建立結果值的函式。
傳回
TResult
型別項目的集合,其中每個項目都代表群組及其索引鍵的投影。
例外狀況
source
、keySelector
或 resultSelector
為 null
。
範例
下列程式碼範例示範如何使用 GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>) 來分組序列的專案,並投影 類型 TResult
的結果序列。
class Pet
{
public string Name { get; set; }
public double Age { get; set; }
}
public static void GroupByEx3()
{
// Create a list of pets.
List<Pet> petsList =
new List<Pet>{ new Pet { Name="Barley", Age=8.3 },
new Pet { Name="Boots", Age=4.9 },
new Pet { Name="Whiskers", Age=1.5 },
new Pet { Name="Daisy", Age=4.3 } };
// Group Pet objects by the Math.Floor of their age.
// Then project an anonymous type from each group
// that consists of the key, the count of the group's
// elements, and the minimum and maximum age in the group.
var query = petsList.GroupBy(
pet => Math.Floor(pet.Age),
(age, pets) => new
{
Key = age,
Count = pets.Count(),
Min = pets.Min(pet => pet.Age),
Max = pets.Max(pet => pet.Age)
});
// Iterate over each anonymous type.
foreach (var result in query)
{
Console.WriteLine("\nAge group: " + result.Key);
Console.WriteLine("Number of pets in this age group: " + result.Count);
Console.WriteLine("Minimum age: " + result.Min);
Console.WriteLine("Maximum age: " + result.Max);
}
/* This code produces the following output:
Age group: 8
Number of pets in this age group: 1
Minimum age: 8.3
Maximum age: 8.3
Age group: 4
Number of pets in this age group: 2
Minimum age: 4.3
Maximum age: 4.9
Age group: 1
Number of pets in this age group: 1
Minimum age: 1.5
Maximum age: 1.5
*/
}
Structure Pet
Public Name As String
Public Age As Double
End Structure
Public Sub GroupByEx3()
' Create a list of pets.
Dim petsList As New List(Of Pet)(New Pet() _
{New Pet With {.Name = "Barley", .Age = 8.3},
New Pet With {.Name = "Boots", .Age = 4.9},
New Pet With {.Name = "Whiskers", .Age = 1.5},
New Pet With {.Name = "Daisy", .Age = 4.3}})
' Group Pet objects by the Math.Floor of their age.
' Then project an anonymous type from each group
' that consists of the key, the count of the group's
' elements, and the minimum and maximum age in the group.
Dim query = petsList.GroupBy(
Function(pet) Math.Floor(pet.Age),
Function(age, pets) New With
{.Key = age,
.Count = pets.Count(),
.Min = pets.Min(Function(pet) pet.Age),
.Max = pets.Max(Function(Pet) Pet.Age)}
)
Dim output As New System.Text.StringBuilder
' Iterate over each anonymous type.
For Each result In query
output.AppendLine(vbCrLf & "Age group: " & result.Key)
output.AppendLine("Number of pets in this age group: " & result.Count)
output.AppendLine("Minimum age: " & result.Min)
output.AppendLine("Maximum age: " & result.Max)
Next
' Display the output.
Console.WriteLine(output.ToString)
End Sub
' This code produces the following output:
' Age group: 8
' Number of pets in this age group: 1
' Minimum age: 8.3
' Maximum age: 8.3
'
' Age group: 4
' Number of pets in this age group: 2
' Minimum age: 4.3
' Maximum age: 4.9
'
' Age group: 1
' Number of pets in this age group: 1
' Minimum age: 1.5
' Maximum age: 1.5
備註
在查詢運算式語法中, group by
(C#) 或 Group By Into
(Visual Basic) 子句會轉譯為 的 GroupBy 調用。
另請參閱
適用於
GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>, IEqualityComparer<TKey>)
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
依據指定的索引鍵選取器函式來群組序列的項目,並從每個群組及其索引鍵建立結果值。 索引鍵是使用指定的比較子來進行比較。
public:
generic <typename TSource, typename TKey, typename TResult>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<TResult> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, Func<TKey, System::Collections::Generic::IEnumerable<TSource> ^, TResult> ^ resultSelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Collections.Generic.IEnumerable<TResult> GroupBy<TSource,TKey,TResult> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, Func<TKey,System.Collections.Generic.IEnumerable<TSource>,TResult> resultSelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * Func<'Key, seq<'Source>, 'Result> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<'Result>
<Extension()>
Public Function GroupBy(Of TSource, TKey, TResult) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), resultSelector As Func(Of TKey, IEnumerable(Of TSource), TResult), comparer As IEqualityComparer(Of TKey)) As IEnumerable(Of TResult)
類型參數
- TSource
source
項目的類型。
- TKey
keySelector
所傳回之索引鍵的型別。
- TResult
resultSelector
所傳回之結果值的型別。
參數
- source
- IEnumerable<TSource>
要群組其項目的 IEnumerable<T>。
- keySelector
- Func<TSource,TKey>
用來擷取各項目之索引鍵的函式。
- resultSelector
- Func<TKey,IEnumerable<TSource>,TResult>
用來從各個群組建立結果值的函式。
- comparer
- IEqualityComparer<TKey>
用來比較索引鍵的 IEqualityComparer<T>。
傳回
TResult
型別項目的集合,其中每個項目都代表群組及其索引鍵的投影。
例外狀況
source
、keySelector
或 resultSelector
為 null
。
另請參閱
適用於
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
依據指定的索引鍵選擇器函式來群組序列的項目。
public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Linq::IGrouping<TKey, TSource> ^> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> -> seq<System.Linq.IGrouping<'Key, 'Source>>
<Extension()>
Public Function GroupBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey)) As IEnumerable(Of IGrouping(Of TKey, TSource))
類型參數
- TSource
source
項目的類型。
- TKey
keySelector
所傳回之索引鍵的型別。
參數
- source
- IEnumerable<TSource>
要群組其項目的 IEnumerable<T>。
- keySelector
- Func<TSource,TKey>
用來擷取各項目之索引鍵的函式。
傳回
C# 或 IEnumerable(Of IGrouping(Of TKey, TSource))
Visual Basic 中的 , IEnumerable<IGrouping<TKey, TSource>>
其中每個 IGrouping<TKey,TElement> 物件都包含一連串的物件和索引鍵。
例外狀況
source
或 keySelector
為 null
。
備註
此方法是使用延後執行來實作。 立即傳回值是物件,可儲存執行動作所需的所有資訊。 除非直接在 GetEnumerator
C# 或 Visual Basic 中使用 foreach
來列舉物件,否則 For Each
不會執行這個方法所表示的查詢。
注意
如需 的 GroupBy
範例,請參閱下列文章:
- GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
- GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)
- GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
方法 GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) 會傳回 物件的集合 IGrouping<TKey,TElement> ,針對所遇到的每個不同索引鍵各傳回一個。 IGrouping<TKey,TElement>是 , IEnumerable<T> 它也有與其專案相關聯的索引鍵。
根據中產生每個 IGrouping<TKey,TElement> 之第一個索引鍵的專案 source
順序,產生 IGrouping<TKey,TElement> 物件的順序。 群組中的元素會依出現在 source
的順序產生。
預設相等比較子 Default 是用來比較索引鍵。
在查詢運算式語法中, group by
(C#) 或 Group By Into
(Visual Basic) 子句會轉譯為 的 GroupBy 調用。 如需詳細資訊和使用範例,請參閱 group 子句 和 Group By 子句。
另請參閱
適用於
GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
- 來源:
- Grouping.cs
依據指定的索引鍵選取器函式來群組序列的項目,並使用指定的比較子來比較索引鍵。
public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
static System::Collections::Generic::IEnumerable<System::Linq::IGrouping<TKey, TSource> ^> ^ GroupBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IEqualityComparer<TKey> ^ comparer);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey> comparer);
public static System.Collections.Generic.IEnumerable<System.Linq.IGrouping<TKey,TSource>> GroupBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IEqualityComparer<TKey>? comparer);
static member GroupBy : seq<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IEqualityComparer<'Key> -> seq<System.Linq.IGrouping<'Key, 'Source>>
<Extension()>
Public Function GroupBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), comparer As IEqualityComparer(Of TKey)) As IEnumerable(Of IGrouping(Of TKey, TSource))
類型參數
- TSource
source
項目的類型。
- TKey
keySelector
所傳回之索引鍵的型別。
參數
- source
- IEnumerable<TSource>
要群組其項目的 IEnumerable<T>。
- keySelector
- Func<TSource,TKey>
用來擷取各項目之索引鍵的函式。
- comparer
- IEqualityComparer<TKey>
用來比較金鑰的 IEqualityComparer<T>。
傳回
C# 或 IEnumerable(Of IGrouping(Of TKey, TSource))
Visual Basic 中的 , IEnumerable<IGrouping<TKey, TSource>>
其中每個 IGrouping<TKey,TElement> 物件都包含 物件集合和索引鍵。
例外狀況
source
或 keySelector
為 null
。
備註
此方法是使用延後執行來實作。 立即傳回值是物件,可儲存執行動作所需的所有資訊。 除非直接在 GetEnumerator
C# 或 Visual Basic 中使用 foreach
來列舉物件,否則 For Each
不會執行這個方法所表示的查詢。
注意
如需 的 GroupBy
範例,請參閱下列文章:
- GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)
- GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)
- GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)
方法 GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>) 會傳回 物件的集合 IGrouping<TKey,TElement> ,針對所遇到的每個不同索引鍵各傳回一個。 IGrouping<TKey,TElement>是 , IEnumerable<T> 它也有與其專案相關聯的索引鍵。
根據中產生每個 IGrouping<TKey,TElement> 之第一個索引鍵的專案 source
順序,產生 IGrouping<TKey,TElement> 物件的順序。 群組中的元素會依出現在 source
的順序產生。
如果 comparer
為 null
,則會使用預設相等比較子 Default 來比較索引鍵。
如果根據 將兩個索引鍵視為相等 comparer
,則會選擇第一個索引鍵做為該群組的索引鍵。
在查詢運算式語法中, group by
(C#) 或 Group By Into
(Visual Basic) 子句會轉譯為 的 GroupBy 調用。 如需詳細資訊和使用範例,請參閱 group 子句 和 Group By 子句。