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
- ソース:
- 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
- ソース:
- 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
- ソース:
- 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<IGrouping<TKey, TElement>> の IEnumerable(Of IGrouping(Of TKey, TElement)) Visual Basic 。各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
Note
C# または Visual Basic クエリ式では、要素とキーの選択式は、GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) メソッドの呼び出しにおける引数の位置とは逆の順序で発生します。
注釈
このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 このメソッドで表されるクエリは、GetEnumerator メソッドを直接呼び出すか、C# で foreach を使用するか、Visual Basic で For Each を使用して列挙されるまで実行されません。
GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) メソッドは、検出された個別のキーごとに 1 つずつ、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
- ソース:
- 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<IGrouping<TKey, TElement>> の IEnumerable(Of IGrouping(Of TKey, TElement)) Visual Basic 。各IGrouping<TKey,TElement> オブジェクトには、TElement 型のオブジェクトのコレクションとキーが含まれています。
例外
source または keySelector または elementSelector が null。
注釈
このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 このメソッドで表されるクエリは、GetEnumerator メソッドを直接呼び出すか、C# で foreach を使用するか、Visual Basic で For Each を使用して列挙されるまで実行されません。
Note
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>) メソッドは、検出された個別のキーごとに 1 つずつ、IGrouping<TKey,TElement> オブジェクトのコレクションを返します。 IGrouping<TKey,TElement>は、その要素に関連付けられたキーを持つIEnumerable<T>です。
IGrouping<TKey,TElement> オブジェクトは、各sourceの最初のキーを生成したIGrouping<TKey,TElement>内の要素の順序に基づいて順序で生成されます。 グループ化内の要素は、生成された要素が sourceに表示される順序で生成されます。
comparerがnullの場合、キーの比較には、既定の等値比較子Defaultが使用されます。
comparerに従って 2 つのキーが等しいと見なされる場合は、そのグループ化のキーとして最初のキーが選択されます。
クエリ式の構文では、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
- ソース:
- 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
- ソース:
- 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
- ソース:
- 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# または Visual Basic のIEnumerable<IGrouping<TKey, TSource>>のIEnumerable(Of IGrouping(Of TKey, TSource))。各IGrouping<TKey,TElement> オブジェクトには、一連のオブジェクトとキーが含まれます。
例外
source または keySelector が null。
注釈
このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 このメソッドで表されるクエリは、GetEnumerator メソッドを直接呼び出すか、C# で foreach を使用するか、Visual Basic で For Each を使用して列挙されるまで実行されません。
Note
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>) メソッドは、検出された個別のキーごとに 1 つずつ、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
- ソース:
- 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<IGrouping<TKey, TSource>> のIEnumerable(Of IGrouping(Of TKey, TSource)) Visual Basic 。各IGrouping<TKey,TElement> オブジェクトには、オブジェクトとキーのコレクションが含まれています。
例外
source または keySelector が null。
注釈
このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 このメソッドで表されるクエリは、GetEnumerator メソッドを直接呼び出すか、C# で foreach を使用するか、Visual Basic で For Each を使用して列挙されるまで実行されません。
Note
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>) メソッドは、検出された個別のキーごとに 1 つずつ、IGrouping<TKey,TElement> オブジェクトのコレクションを返します。 IGrouping<TKey,TElement>は、その要素に関連付けられたキーを持つIEnumerable<T>です。
IGrouping<TKey,TElement> オブジェクトは、各sourceの最初のキーを生成したIGrouping<TKey,TElement>内の要素の順序に基づいて順序で生成されます。 グループ化内の要素は、 sourceに表示される順序で生成されます。
comparerがnullの場合、キーの比較には、既定の等値比較子Defaultが使用されます。
comparerに従って 2 つのキーが等しいと見なされる場合は、そのグループ化のキーとして最初のキーが選択されます。
クエリ式の構文では、group by (C#) または Group By Into (Visual Basic) 句は、GroupBy の呼び出しに変換されます。 詳細と使用例については、 group 句 と Group By 句を参照してください。