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 型別項目的集合,其中每個項目都代表群組及其索引鍵的投影。

例外狀況

sourcekeySelectorelementSelectorresultSelectornull

範例

下列程式碼範例示範如何使用 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 型別項目的集合,其中每個項目都代表群組及其索引鍵的投影。

例外狀況

sourcekeySelectorelementSelectorresultSelectornull

另請參閱

適用於

.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>>

C# 或 IEnumerable(Of IGrouping(Of TKey, TElement)) Visual Basic 中的 , IEnumerable<IGrouping<TKey, TElement>> 其中每個 IGrouping<TKey,TElement> 物件都包含型 TElement 別和索引鍵的物件集合。

例外狀況

sourcekeySelectorelementSelectornull

範例

下列程式碼範例示範如何使用 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# 或 foreachFor 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 是用來比較索引鍵。

另請參閱

適用於

.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>>

C# 或 IEnumerable(Of IGrouping(Of TKey, TElement)) Visual Basic 中的 , IEnumerable<IGrouping<TKey, TElement>> 其中每個 IGrouping<TKey,TElement> 物件都包含 型 TElement 別和索引鍵的物件集合。

例外狀況

sourcekeySelectorelementSelectornull

備註

這個方法是使用延後執行來實作。 立即傳回值是一個物件,會儲存執行動作所需的所有資訊。 除非直接呼叫其 GetEnumerator 方法或在 C# 或 foreachFor Each Visual Basic 中使用 來列舉物件,否則不會執行這個方法所代表的查詢。

方法 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

如果 為 comparernull ,則會使用預設相等比較子 Default 來比較索引鍵。

如果兩個索引鍵視為相等, comparer 則會選擇第一個索引鍵作為該群組的索引鍵。

在查詢運算式語法中, 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 型別項目的集合,其中每個項目都代表群組及其索引鍵的投影。

例外狀況

sourcekeySelectorresultSelectornull

範例

下列程式碼範例示範如何使用 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 型別項目的集合,其中每個項目都代表群組及其索引鍵的投影。

例外狀況

sourcekeySelectorresultSelectornull

另請參閱

適用於

.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>>

C# 或 IEnumerable(Of IGrouping(Of TKey, TSource)) Visual Basic 中的 , IEnumerable<IGrouping<TKey, TSource>> 其中每個 IGrouping<TKey,TElement> 物件都包含一連串的物件和索引鍵。

例外狀況

sourcekeySelectornull

備註

此方法是使用延後執行來實作。 立即傳回值是物件,可儲存執行動作所需的所有資訊。 除非直接在 GetEnumerator C# 或 Visual Basic 中使用 foreach 來列舉物件,否則 For Each 不會執行這個方法所表示的查詢。

方法 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 子句

另請參閱

適用於

.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>>

C# 或 IEnumerable(Of IGrouping(Of TKey, TSource)) Visual Basic 中的 , IEnumerable<IGrouping<TKey, TSource>> 其中每個 IGrouping<TKey,TElement> 物件都包含 物件集合和索引鍵。

例外狀況

sourcekeySelectornull

備註

此方法是使用延後執行來實作。 立即傳回值是物件,可儲存執行動作所需的所有資訊。 除非直接在 GetEnumerator C# 或 Visual Basic 中使用 foreach 來列舉物件,否則 For Each 不會執行這個方法所表示的查詢。

方法 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 的順序產生。

如果 comparernull ,則會使用預設相等比較子 Default 來比較索引鍵。

如果根據 將兩個索引鍵視為相等 comparer ,則會選擇第一個索引鍵做為該群組的索引鍵。

在查詢運算式語法中, 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