英語で読む

次の方法で共有


Enumerable.GroupBy メソッド

定義

シーケンスの要素をグループ化します。

オーバーロード

GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 各グループの要素は、指定された関数を使用して射影されます。

GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource, TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>, TResult>, IEqualityComparer<TKey>)

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 キー値の比較には、指定された比較子を使用し、各グループの要素の射影には、指定された関数を使用します。

GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、指定された関数を使用して各グループの要素を射影します。

GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)

キー セレクター関数に従ってシーケンスの要素をグループ化します。 キーの比較には、比較子を使用し、各グループの要素の射影には、指定された関数を使用します。

GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。

GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>, IEqualityComparer<TKey>)

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 キーの比較には、指定された比較子を使用します。

GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化します。

GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、指定された比較子を使用してキーを比較します。

GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>)

ソース:
Grouping.cs
ソース:
Grouping.cs
ソース:
Grouping.cs

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 各グループの要素は、指定された関数を使用して射影されます。

C#
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);

型パラメーター

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>

各グループから結果値を作成する関数。

戻り値

IEnumerable<TResult>

TResult 型の要素のコレクション。各要素は、グループとそのキーの射影を表します。

例外

sourcekeySelectorelementSelector、または resultSelectornull です。

次のコード例では、 を使用 GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>) してシーケンスの投影された要素をグループ化し、 型 TResultの結果のシーケンスを投影する方法を示します。

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

注釈

クエリ式の構文では、 group by (C#) 句または Group By Into (Visual Basic) 句が の GroupBy呼び出しに変換されます。

こちらもご覧ください

適用対象

.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

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

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 キー値の比較には、指定された比較子を使用し、各グループの要素の射影には、指定された関数を使用します。

C#
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);
C#
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);

型パラメーター

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>

戻り値

IEnumerable<TResult>

TResult 型の要素のコレクション。各要素は、グループとそのキーの射影を表します。

例外

sourcekeySelectorelementSelector、または resultSelectornull です。

こちらもご覧ください

適用対象

.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

GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>)

ソース:
Grouping.cs
ソース:
Grouping.cs
ソース:
Grouping.cs

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、指定された関数を使用して各グループの要素を射影します。

C#
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);

型パラメーター

TSource

source の要素の型。

TKey

keySelector によって返されるキーの型。

TElement

IGrouping<TKey,TElement> 内の要素の型。

パラメーター

source
IEnumerable<TSource>

グループ化する要素を含む IEnumerable<T>

keySelector
Func<TSource,TKey>

各要素のキーを抽出する関数。

elementSelector
Func<TSource,TElement>

ソースの各要素を IGrouping<TKey,TElement> の要素に割り当てる関数。

戻り値

IEnumerable<IGrouping<TKey,TElement>>

IEnumerable<IGrouping<TKey, TElement>> C# または IEnumerable(Of IGrouping(Of TKey, TElement)) Visual Basic の 。 各IGrouping<TKey,TElement>オブジェクトに型TElementとキーのオブジェクトのコレクションが含まれています。

例外

sourcekeySelector、または elementSelector は、null です。

次のコード例では、 を使用 GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) してシーケンスの要素をグループ化する方法を示します。

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

クエリ式の構文では、 group by (C#) 句または Group By Into (Visual Basic) 句が の GroupBy呼び出しに変換されます。 次の例のクエリ式の変換は、上記の例のクエリと同じです。

C#
IEnumerable<IGrouping<int, string>> query =
    from pet in pets
    group pet.Name by pet.Age;

注意

C# または Visual Basic のクエリ式では、要素とキーの選択式は、 メソッドの呼び出し GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) で引数の位置から逆の順序で行われます。

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出GetEnumeratorすか、C# または For Each Visual Basic で を使用foreachして列挙されるまで実行されません。

メソッドは GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) 、検出された個別のキーごとに 1 つずつ、オブジェクトの IGrouping<TKey,TElement> コレクションを返します。 IGrouping<TKey,TElement>は、 IEnumerable<T> 要素に関連付けられたキーも持つ です。

オブジェクトはIGrouping<TKey,TElement>、各 IGrouping<TKey,TElement>の最初のキーを生成した 内sourceの要素の順序に基づいて、順序で生成されます。 グループ内の要素は、生成された要素が に表示される順序で source生成されます。

キーの比較には、既定の等値比較子 Default が使用されます。

こちらもご覧ください

適用対象

.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

GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>)

ソース:
Grouping.cs
ソース:
Grouping.cs
ソース:
Grouping.cs

キー セレクター関数に従ってシーケンスの要素をグループ化します。 キーの比較には、比較子を使用し、各グループの要素の射影には、指定された関数を使用します。

C#
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);
C#
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);

型パラメーター

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>

戻り値

IEnumerable<IGrouping<TKey,TElement>>

IEnumerable<IGrouping<TKey, TElement>> C# または IEnumerable(Of IGrouping(Of TKey, TElement)) Visual Basic の 。各IGrouping<TKey,TElement>オブジェクトに型TElementとキーのオブジェクトのコレクションが含まれています。

例外

sourcekeySelector、または elementSelector は、null です。

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出GetEnumeratorすか、C# または For Each Visual Basic で を使用foreachして列挙されるまで実行されません。

メソッドは 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>、各 IGrouping<TKey,TElement>の最初のキーを生成した 内sourceの要素の順序に基づいて、順序で生成されます。 グループ内の要素は、生成された要素が に表示される順序で source生成されます。

が のnull場合comparer、キーの比較には既定の等値比較子Defaultが使用されます。

に従って comparer2 つのキーが等しいと見なされる場合、最初のキーがそのグループ化のキーとして選択されます。

クエリ式の構文では、 group by (C#) 句または Group By Into (Visual Basic) 句が の GroupBy呼び出しに変換されます。 詳細と使用例については、 group 句Group By 句に関するページを参照してください。

こちらもご覧ください

適用対象

.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

GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>)

ソース:
Grouping.cs
ソース:
Grouping.cs
ソース:
Grouping.cs

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。

C#
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);

型パラメーター

TSource

source の要素の型。

TKey

keySelector によって返されるキーの型。

TResult

resultSelector によって返される結果値の型。

パラメーター

source
IEnumerable<TSource>

グループ化する要素を含む IEnumerable<T>

keySelector
Func<TSource,TKey>

各要素のキーを抽出する関数。

resultSelector
Func<TKey,IEnumerable<TSource>,TResult>

各グループから結果値を作成する関数。

戻り値

IEnumerable<TResult>

TResult 型の要素のコレクション。各要素は、グループとそのキーの射影を表します。

例外

sourcekeySelector、または resultSelector は、null です。

次のコード例では、 を使用 GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>) してシーケンスの要素をグループ化し、 型 TResultの結果のシーケンスを投影する方法を示します。

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

注釈

クエリ式の構文では、 group by (C#) 句または Group By Into (Visual Basic) 句が の GroupBy呼び出しに変換されます。

こちらもご覧ください

適用対象

.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

GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>, IEqualityComparer<TKey>)

ソース:
Grouping.cs
ソース:
Grouping.cs
ソース:
Grouping.cs

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、各グループとそのキーから結果値を作成します。 キーの比較には、指定された比較子を使用します。

C#
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);
C#
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);

型パラメーター

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>

戻り値

IEnumerable<TResult>

TResult 型の要素のコレクション。各要素は、グループとそのキーの射影を表します。

例外

sourcekeySelector、または resultSelector は、null です。

こちらもご覧ください

適用対象

.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

GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)

ソース:
Grouping.cs
ソース:
Grouping.cs
ソース:
Grouping.cs

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化します。

C#
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);

型パラメーター

TSource

source の要素の型。

TKey

keySelector によって返されるキーの型。

パラメーター

source
IEnumerable<TSource>

グループ化する要素を含む IEnumerable<T>

keySelector
Func<TSource,TKey>

各要素のキーを抽出する関数。

戻り値

IEnumerable<IGrouping<TKey,TSource>>

IEnumerable<IGrouping<TKey, TSource>> C# または IEnumerable(Of IGrouping(Of TKey, TSource)) Visual Basic の 。各IGrouping<TKey,TElement>オブジェクトに一連のオブジェクトとキーが含まれています。

例外

source または keySelectornull です。

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出GetEnumeratorすか、C# For Each または Visual Basic で を使用foreachして列挙されるまで実行されません。

メソッドは GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) 、検出された個別のキーごとに 1 つずつ、オブジェクトの IGrouping<TKey,TElement> コレクションを返します。 IGrouping<TKey,TElement>は、IEnumerable<T>その要素に関連付けられたキーも持つ です。

オブジェクトはIGrouping<TKey,TElement>、各 IGrouping<TKey,TElement>の最初のキーを生成した 内sourceの要素の順序に基づいて順序で生成されます。 グループ化内の要素は、 に表示される順序で source生成されます。

キーの比較には、既定の等値比較子 Default が使用されます。

クエリ式の構文では、 group by (C#) 句または Group By Into (Visual Basic) 句が の GroupBy呼び出しに変換されます。 詳細と使用例については、 group 句Group By 句に関するページを参照してください。

こちらもご覧ください

適用対象

.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

GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>)

ソース:
Grouping.cs
ソース:
Grouping.cs
ソース:
Grouping.cs

指定されたキー セレクター関数に従ってシーケンスの要素をグループ化し、指定された比較子を使用してキーを比較します。

C#
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);
C#
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);

型パラメーター

TSource

source の要素の型。

TKey

keySelector によって返されるキーの型。

パラメーター

source
IEnumerable<TSource>

グループ化する要素を含む IEnumerable<T>

keySelector
Func<TSource,TKey>

各要素のキーを抽出する関数。

comparer
IEqualityComparer<TKey>

キーを比較する IEqualityComparer<T>

戻り値

IEnumerable<IGrouping<TKey,TSource>>

IEnumerable<IGrouping<TKey, TSource>> C# または IEnumerable(Of IGrouping(Of TKey, TSource)) Visual Basic の 。各IGrouping<TKey,TElement>オブジェクトにオブジェクトとキーのコレクションが含まれています。

例外

source または keySelectornull です。

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納する オブジェクトです。 このメソッドで表されるクエリは、オブジェクトがメソッドを直接呼び出GetEnumeratorすか、C# For Each または Visual Basic で を使用foreachして列挙されるまで実行されません。

メソッドは GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>) 、検出された個別のキーごとに 1 つずつ、オブジェクトの IGrouping<TKey,TElement> コレクションを返します。 IGrouping<TKey,TElement>は、IEnumerable<T>その要素に関連付けられたキーも持つ です。

オブジェクトはIGrouping<TKey,TElement>、各 IGrouping<TKey,TElement>の最初のキーを生成した 内sourceの要素の順序に基づいて順序で生成されます。 グループ化内の要素は、 に表示される順序で source生成されます。

nullの場合comparer、キーの比較に既定の等値比較子Defaultが使用されます。

に従って comparer2 つのキーが等しいと見なされる場合、最初のキーがそのグループ化のキーとして選択されます。

クエリ式の構文では、 group by (C#) 句または Group By Into (Visual Basic) 句が の GroupBy呼び出しに変換されます。 詳細と使用例については、 group 句Group By 句に関するページを参照してください。

こちらもご覧ください

適用対象

.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