Enumerable.GroupBy Enumerable.GroupBy Enumerable.GroupBy Enumerable.GroupBy Method

정의

시퀀스의 요소를 그룹화합니다.Groups the elements of a sequence.

오버로드

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

지정된 키 누적기 함수에 따라 시퀀스의 요소를 그룹화하고 각 그룹의 결과 값과 해당 키를 만듭니다.Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. 각 그룹의 요소는 지정된 함수를 통해 투영됩니다.The elements of each group are projected by using a specified function.

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

지정된 키 누적기 함수에 따라 시퀀스의 요소를 그룹화하고 각 그룹의 결과 값과 해당 키를 만듭니다.Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. 키 값은 지정된 비교자를 통해 비교되고 각 그룹의 요소는 지정된 함수를 통해 투영됩니다.Key values are compared by using a specified comparer, and the elements of each group are projected by using a specified function.

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

지정된 키 선택기 함수에 따라 시퀀스의 요소를 그룹화하고 지정된 함수를 사용하여 각 그룹의 요소를 투영합니다.Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.

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

키 선택기 함수에 따라 시퀀스의 요소를 그룹화합니다.Groups the elements of a sequence according to a key selector function. 키는 비교자를 통해 비교되고 각 그룹의 요소는 지정된 함수를 통해 투영됩니다.The keys are compared by using a comparer and each group's elements are projected by using a specified function.

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

지정된 키 누적기 함수에 따라 시퀀스의 요소를 그룹화하고 각 그룹의 결과 값과 해당 키를 만듭니다.Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key.

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

지정된 키 누적기 함수에 따라 시퀀스의 요소를 그룹화하고 각 그룹의 결과 값과 해당 키를 만듭니다.Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. 키는 지정된 비교자를 통해 비교됩니다.The keys are compared by using a specified comparer.

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

지정된 키 선택기 함수에 따라 시퀀스의 요소를 그룹화합니다.Groups the elements of a sequence according to a specified key selector function.

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

지정된 키 선택기 함수에 따라 지정된 비교자로 키를 비교하여 시퀀스의 요소를 그룹화합니다.Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.

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

지정된 키 누적기 함수에 따라 시퀀스의 요소를 그룹화하고 각 그룹의 결과 값과 해당 키를 만듭니다.Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. 각 그룹의 요소는 지정된 함수를 통해 투영됩니다.The elements of each group are projected by using a specified function.

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 요소의 형식입니다.The type of the elements of source.

TKey

keySelector에서 반환하는 키의 형식입니다.The type of the key returned by keySelector.

TElement

IGrouping<TKey,TElement>에 있는 요소의 형식입니다.The type of the elements in each IGrouping<TKey,TElement>.

TResult

resultSelector에서 반환하는 결과 값의 형식입니다.The type of the result value returned by resultSelector.

매개 변수

source
IEnumerable<TSource>

요소를 그룹화할 IEnumerable<T>입니다.An IEnumerable<T> whose elements to group.

keySelector
Func<TSource,TKey>

각 요소에 대해 키를 추출하는 함수입니다.A function to extract the key for each element.

elementSelector
Func<TSource,TElement>

각 소스 요소를 IGrouping<TKey,TElement>의 요소에 매핑하는 함수입니다.A function to map each source element to an element in an IGrouping<TKey,TElement>.

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

각 그룹의 결과 값을 만드는 함수입니다.A function to create a result value from each group.

반환

IEnumerable<TResult>

각 요소가 그룹과 해당 키에 대한 프로젝션을 나타내는 TResult 형식의 요소 컬렉션입니다.A collection of elements of type TResult where each element represents a projection over a group and its key.

예제

다음 코드 예제에 사용 하는 방법을 보여 줍니다 GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>) 프로젝션 된 요소의 시퀀스를 그룹화 하 고 다음 유형의 결과의 시퀀스를 프로젝트에 TResult입니다.The following code example demonstrates how to use GroupBy<TSource,TKey,TElement,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, Func<TKey,IEnumerable<TElement>,TResult>) to group the projected elements of a sequence and then project a sequence of results of type 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.
    MsgBox(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 (Visual C#) 또는 Group By Into 절 (Visual Basic)의 호출으로 변환 GroupBy합니다.In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy.

추가 정보

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

지정된 키 누적기 함수에 따라 시퀀스의 요소를 그룹화하고 각 그룹의 결과 값과 해당 키를 만듭니다.Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. 키 값은 지정된 비교자를 통해 비교되고 각 그룹의 요소는 지정된 함수를 통해 투영됩니다.Key values are compared by using a specified comparer, and the elements of each group are projected by using a specified function.

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);
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 요소의 형식입니다.The type of the elements of source.

TKey

keySelector에서 반환하는 키의 형식입니다.The type of the key returned by keySelector.

TElement

IGrouping<TKey,TElement>에 있는 요소의 형식입니다.The type of the elements in each IGrouping<TKey,TElement>.

TResult

resultSelector에서 반환하는 결과 값의 형식입니다.The type of the result value returned by resultSelector.

매개 변수

source
IEnumerable<TSource>

요소를 그룹화할 IEnumerable<T>입니다.An IEnumerable<T> whose elements to group.

keySelector
Func<TSource,TKey>

각 요소에 대해 키를 추출하는 함수입니다.A function to extract the key for each element.

elementSelector
Func<TSource,TElement>

각 소스 요소를 IGrouping<TKey,TElement>의 요소에 매핑하는 함수입니다.A function to map each source element to an element in an IGrouping<TKey,TElement>.

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

각 그룹의 결과 값을 만드는 함수입니다.A function to create a result value from each group.

comparer
IEqualityComparer<TKey>

키를 비교할 IEqualityComparer<T>입니다.An IEqualityComparer<T> to compare keys with.

반환

IEnumerable<TResult>

각 요소가 그룹과 해당 키에 대한 프로젝션을 나타내는 TResult 형식의 요소 컬렉션입니다.A collection of elements of type TResult where each element represents a projection over a group and its key.

추가 정보

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

지정된 키 선택기 함수에 따라 시퀀스의 요소를 그룹화하고 지정된 함수를 사용하여 각 그룹의 요소를 투영합니다.Groups the elements of a sequence according to a specified key selector function and projects the elements for each group by using a specified function.

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 요소의 형식입니다.The type of the elements of source.

TKey

keySelector에서 반환하는 키의 형식입니다.The type of the key returned by keySelector.

TElement

IGrouping<TKey,TElement> 요소의 형식입니다.The type of the elements in the IGrouping<TKey,TElement>.

매개 변수

source
IEnumerable<TSource>

요소를 그룹화할 IEnumerable<T>입니다.An IEnumerable<T> whose elements to group.

keySelector
Func<TSource,TKey>

각 요소에 대해 키를 추출하는 함수입니다.A function to extract the key for each element.

elementSelector
Func<TSource,TElement>

각 소스 요소를 IGrouping<TKey,TElement>의 요소에 매핑하는 함수입니다.A function to map each source element to an element in the IGrouping<TKey,TElement>.

반환

IEnumerable<IGrouping<TKey,TElement>>

IGrouping<TKey,TElement> 개체에 TElement 형식 개체 및 키의 컬렉션이 들어 있는 IEnumerable<IGrouping<TKey, TElement>>(C#의 경우) 또는 IEnumerable(Of IGrouping(Of TKey, TElement))(Visual Basic의 경우)입니다.An IEnumerable<IGrouping<TKey, TElement>> in C# or IEnumerable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each IGrouping<TKey,TElement> object contains a collection of objects of type TElement and a key.

예외

source, keySelector 또는 elementSelectornull인 경우source or keySelector or elementSelector is null.

예제

다음 코드 예제에 사용 하는 방법을 보여 줍니다. GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) 시퀀스의 요소를 그룹화 합니다.The following code example demonstrates how to use GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) to group the elements of a sequence.

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.
    MsgBox(output.ToString)
End Sub

' This code produces the following output:
'
' 8
'   Barley
' 4
'   Boots
'   Daisy
' 1
'   Whiskers

쿼리 식 구문에는 group by (Visual C#) 또는 Group By Into 절 (Visual Basic)의 호출으로 변환 GroupBy합니다.In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy. 다음 예제에서 쿼리 식 변환 위의 예제에서 쿼리 하는 것과 같습니다.The translation of the query expression in the following example is equivalent to the query in the example above.

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

참고

Visual C# 또는 Visual Basic 쿼리 식에서 요소 및 키 선택 식 역순으로에서 발생에 대 한 호출의 인수 위치는 GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) 메서드.In a Visual C# or Visual Basic query expression, the element and key selection expressions occur in the reverse order from their argument positions in a call to the GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) method.

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다.This method is implemented by using deferred execution. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다.The immediate return value is an object that stores all the information that is required to perform the action. 이 메서드를 나타내는 쿼리 하거나 호출 하 여 개체 열거 될 때까지 실행 되지 않습니다 해당 GetEnumerator 메서드를 사용 하 여 직접 또는 foreach Visual C# 또는 For Each Visual Basic의 합니다.The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

합니다 GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) 의 컬렉션을 반환 하는 메서드 IGrouping<TKey,TElement> 개체에서 발생 하는 각 고유 키에 대 한 합니다.The GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>) method returns a collection of IGrouping<TKey,TElement> objects, one for each distinct key that was encountered. IGrouping<TKey,TElement> 되는 IEnumerable<T> 역시 해당 요소와 연결 된 키입니다.An IGrouping<TKey,TElement> is an IEnumerable<T> that also has a key associated with its elements.

합니다 IGrouping<TKey,TElement> 개체의 요소 순서를 기반으로 하는 순서로 생성 되기 source 각각의 첫 번째 키를 생성 하는 IGrouping<TKey,TElement>합니다.The IGrouping<TKey,TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey,TElement>. 그룹의 요소는 생성 하는 요소에 나타나는 순서 대로 생성 됩니다 source합니다.Elements in a grouping are yielded in the order that the elements that produced them appear in source.

기본 같음 비교자를 Default 키를 비교 하는 데 사용 됩니다.The default equality comparer Default is used to compare keys.

추가 정보

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

키 선택기 함수에 따라 시퀀스의 요소를 그룹화합니다.Groups the elements of a sequence according to a key selector function. 키는 비교자를 통해 비교되고 각 그룹의 요소는 지정된 함수를 통해 투영됩니다.The keys are compared by using a comparer and each group's elements are projected by using a specified function.

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);
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 요소의 형식입니다.The type of the elements of source.

TKey

keySelector에서 반환하는 키의 형식입니다.The type of the key returned by keySelector.

TElement

IGrouping<TKey,TElement> 요소의 형식입니다.The type of the elements in the IGrouping<TKey,TElement>.

매개 변수

source
IEnumerable<TSource>

요소를 그룹화할 IEnumerable<T>입니다.An IEnumerable<T> whose elements to group.

keySelector
Func<TSource,TKey>

각 요소에 대해 키를 추출하는 함수입니다.A function to extract the key for each element.

elementSelector
Func<TSource,TElement>

각 소스 요소를 IGrouping<TKey,TElement>의 요소에 매핑하는 함수입니다.A function to map each source element to an element in an IGrouping<TKey,TElement>.

comparer
IEqualityComparer<TKey>

키를 비교할 IEqualityComparer<T>입니다.An IEqualityComparer<T> to compare keys.

반환

IEnumerable<IGrouping<TKey,TElement>>

IGrouping<TKey,TElement> 개체에 TElement 형식 개체 및 키의 컬렉션이 들어 있는 IEnumerable<IGrouping<TKey, TElement>>(C#의 경우) 또는 IEnumerable(Of IGrouping(Of TKey, TElement))(Visual Basic의 경우)입니다.An IEnumerable<IGrouping<TKey, TElement>> in C# or IEnumerable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each IGrouping<TKey,TElement> object contains a collection of objects of type TElement and a key.

예외

source, keySelector 또는 elementSelectornull인 경우source or keySelector or elementSelector is null.

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다.This method is implemented by using deferred execution. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다.The immediate return value is an object that stores all the information that is required to perform the action. 이 메서드를 나타내는 쿼리 하거나 호출 하 여 개체 열거 될 때까지 실행 되지 않습니다 해당 GetEnumerator 메서드를 사용 하 여 직접 또는 foreach Visual C# 또는 For Each Visual Basic의 합니다.The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

합니다 GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>) 의 컬렉션을 반환 하는 메서드 IGrouping<TKey,TElement> 개체에서 발생 하는 각 고유 키에 대 한 합니다.The GroupBy<TSource,TKey,TElement>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TSource,TElement>, IEqualityComparer<TKey>) method returns a collection of IGrouping<TKey,TElement> objects, one for each distinct key that was encountered. IGrouping<TKey,TElement> 되는 IEnumerable<T> 역시 해당 요소와 연결 된 키입니다.An IGrouping<TKey,TElement> is an IEnumerable<T> that also has a key associated with its elements.

합니다 IGrouping<TKey,TElement> 개체의 요소 순서를 기반으로 하는 순서로 생성 되기 source 각각의 첫 번째 키를 생성 하는 IGrouping<TKey,TElement>합니다.The IGrouping<TKey,TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey,TElement>. 그룹의 요소는 생성 하는 요소에 나타나는 순서 대로 생성 됩니다 source합니다.Elements in a grouping are yielded in the order that the elements that produced them appear in source.

하는 경우 comparer 됩니다 null, 기본 같음 비교자를 Default 키를 비교 하는 데 사용 됩니다.If comparer is null, the default equality comparer Default is used to compare keys.

두 개의 키에 따라 같은 것으로 간주 됩니다 comparer, 첫 번째 키가 그룹화 키로 선택 됩니다.If two keys are considered equal according to comparer, the first key is chosen as the key for that grouping.

쿼리 식 구문에는 group by (Visual C#) 또는 Group By Into 절 (Visual Basic)의 호출으로 변환 GroupBy합니다.In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy. 자세한 내용 및 사용 예제를 참조 하세요 group 절 하 고 By 절 그룹합니다.For more information and usage examples, see group clause and Group By Clause.

추가 정보

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

지정된 키 누적기 함수에 따라 시퀀스의 요소를 그룹화하고 각 그룹의 결과 값과 해당 키를 만듭니다.Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key.

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 요소의 형식입니다.The type of the elements of source.

TKey

keySelector에서 반환하는 키의 형식입니다.The type of the key returned by keySelector.

TResult

resultSelector에서 반환하는 결과 값의 형식입니다.The type of the result value returned by resultSelector.

매개 변수

source
IEnumerable<TSource>

요소를 그룹화할 IEnumerable<T>입니다.An IEnumerable<T> whose elements to group.

keySelector
Func<TSource,TKey>

각 요소에 대해 키를 추출하는 함수입니다.A function to extract the key for each element.

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

각 그룹의 결과 값을 만드는 함수입니다.A function to create a result value from each group.

반환

IEnumerable<TResult>

각 요소가 그룹과 해당 키에 대한 프로젝션을 나타내는 TResult 형식의 요소 컬렉션입니다.A collection of elements of type TResult where each element represents a projection over a group and its key.

예제

다음 코드 예제에 사용 하는 방법을 보여 줍니다 GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>) 시퀀스의 요소를 그룹화 하 여 프로젝트 형식의 결과의 시퀀스를 TResult입니다.The following code example demonstrates how to use GroupBy<TSource,TKey,TResult>(IEnumerable<TSource>, Func<TSource,TKey>, Func<TKey,IEnumerable<TSource>,TResult>) to group the elements of a sequence and project a sequence of results of type 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.
    MsgBox(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 (Visual C#) 또는 Group By Into 절 (Visual Basic)의 호출으로 변환 GroupBy합니다.In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy.

추가 정보

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

지정된 키 누적기 함수에 따라 시퀀스의 요소를 그룹화하고 각 그룹의 결과 값과 해당 키를 만듭니다.Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. 키는 지정된 비교자를 통해 비교됩니다.The keys are compared by using a specified comparer.

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);
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 요소의 형식입니다.The type of the elements of source.

TKey

keySelector에서 반환하는 키의 형식입니다.The type of the key returned by keySelector.

TResult

resultSelector에서 반환하는 결과 값의 형식입니다.The type of the result value returned by resultSelector.

매개 변수

source
IEnumerable<TSource>

요소를 그룹화할 IEnumerable<T>입니다.An IEnumerable<T> whose elements to group.

keySelector
Func<TSource,TKey>

각 요소에 대해 키를 추출하는 함수입니다.A function to extract the key for each element.

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

각 그룹의 결과 값을 만드는 함수입니다.A function to create a result value from each group.

comparer
IEqualityComparer<TKey>

키를 비교할 IEqualityComparer<T>입니다.An IEqualityComparer<T> to compare keys with.

반환

IEnumerable<TResult>

각 요소가 그룹과 해당 키에 대한 프로젝션을 나타내는 TResult 형식의 요소 컬렉션입니다.A collection of elements of type TResult where each element represents a projection over a group and its key.

추가 정보

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

지정된 키 선택기 함수에 따라 시퀀스의 요소를 그룹화합니다.Groups the elements of a sequence according to a specified key selector function.

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 요소의 형식입니다.The type of the elements of source.

TKey

keySelector에서 반환하는 키의 형식입니다.The type of the key returned by keySelector.

매개 변수

source
IEnumerable<TSource>

요소를 그룹화할 IEnumerable<T>입니다.An IEnumerable<T> whose elements to group.

keySelector
Func<TSource,TKey>

각 요소에 대해 키를 추출하는 함수입니다.A function to extract the key for each element.

반환

IEnumerable<IGrouping<TKey,TSource>>

IGrouping<TKey,TElement> 개체에 개체 및 키의 시퀀스가 들어 있는 IEnumerable<IGrouping<TKey, TSource>>(C#의 경우) 또는 IEnumerable(Of IGrouping(Of TKey, TSource))(Visual Basic의 경우)입니다.An IEnumerable<IGrouping<TKey, TSource>> in C# or IEnumerable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each IGrouping<TKey,TElement> object contains a sequence of objects and a key.

예외

source 또는 keySelectornull인 경우source or keySelector is null.

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다.This method is implemented by using deferred execution. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다.The immediate return value is an object that stores all the information that is required to perform the action. 이 메서드를 나타내는 쿼리 하거나 호출 하 여 개체 열거 될 때까지 실행 되지 않습니다 해당 GetEnumerator 메서드를 사용 하 여 직접 또는 foreach Visual C# 또는 For Each Visual Basic의 합니다.The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

합니다 GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) 의 컬렉션을 반환 하는 메서드 IGrouping<TKey,TElement> 개체에서 발생 하는 각 고유 키에 대 한 합니다.The GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) method returns a collection of IGrouping<TKey,TElement> objects, one for each distinct key that was encountered. IGrouping<TKey,TElement> 되는 IEnumerable<T> 역시 해당 요소와 연결 된 키입니다.An IGrouping<TKey,TElement> is an IEnumerable<T> that also has a key associated with its elements.

합니다 IGrouping<TKey,TElement> 개체의 요소 순서를 기반으로 하는 순서로 생성 되기 source 각각의 첫 번째 키를 생성 하는 IGrouping<TKey,TElement>합니다.The IGrouping<TKey,TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey,TElement>. 그룹의 요소에 순서 대로 생성 됩니다 source합니다.Elements in a grouping are yielded in the order they appear in source.

기본 같음 비교자를 Default 키를 비교 하는 데 사용 됩니다.The default equality comparer Default is used to compare keys.

쿼리 식 구문에는 group by (Visual C#) 또는 Group By Into 절 (Visual Basic)의 호출으로 변환 GroupBy합니다.In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy. 자세한 내용 및 사용 예제를 참조 하세요 group 절 하 고 By 절 그룹합니다.For more information and usage examples, see group clause and Group By Clause.

추가 정보

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

지정된 키 선택기 함수에 따라 지정된 비교자로 키를 비교하여 시퀀스의 요소를 그룹화합니다.Groups the elements of a sequence according to a specified key selector function and compares the keys by using a specified comparer.

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);
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 요소의 형식입니다.The type of the elements of source.

TKey

keySelector에서 반환하는 키의 형식입니다.The type of the key returned by keySelector.

매개 변수

source
IEnumerable<TSource>

요소를 그룹화할 IEnumerable<T>입니다.An IEnumerable<T> whose elements to group.

keySelector
Func<TSource,TKey>

각 요소에 대해 키를 추출하는 함수입니다.A function to extract the key for each element.

comparer
IEqualityComparer<TKey>

키를 비교할 IEqualityComparer<T>입니다.An IEqualityComparer<T> to compare keys.

반환

IEnumerable<IGrouping<TKey,TSource>>

IGrouping<TKey,TElement> 개체에 개체 및 키의 컬렉션이 들어 있는 IEnumerable<IGrouping<TKey, TSource>>(C#의 경우) 또는 IEnumerable(Of IGrouping(Of TKey, TSource))(Visual Basic의 경우)입니다.An IEnumerable<IGrouping<TKey, TSource>> in C# or IEnumerable(Of IGrouping(Of TKey, TSource)) in Visual Basic where each IGrouping<TKey,TElement> object contains a collection of objects and a key.

예외

source 또는 keySelectornull인 경우source or keySelector is null.

설명

이 메서드는 지연 된 실행을 사용 하 여 구현 됩니다.This method is implemented by using deferred execution. 즉시 반환 값은 작업을 수행 하는 데 필요한 모든 정보를 저장 하는 개체입니다.The immediate return value is an object that stores all the information that is required to perform the action. 이 메서드를 나타내는 쿼리 하거나 호출 하 여 개체 열거 될 때까지 실행 되지 않습니다 해당 GetEnumerator 메서드를 사용 하 여 직접 또는 foreach Visual C# 또는 For Each Visual Basic의 합니다.The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

합니다 GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>) 의 컬렉션을 반환 하는 메서드 IGrouping<TKey,TElement> 개체에서 발생 하는 각 고유 키에 대 한 합니다.The GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IEqualityComparer<TKey>) method returns a collection of IGrouping<TKey,TElement> objects, one for each distinct key that was encountered. IGrouping<TKey,TElement> 되는 IEnumerable<T> 역시 해당 요소와 연결 된 키입니다.An IGrouping<TKey,TElement> is an IEnumerable<T> that also has a key associated with its elements.

합니다 IGrouping<TKey,TElement> 개체의 요소 순서를 기반으로 하는 순서로 생성 되기 source 각각의 첫 번째 키를 생성 하는 IGrouping<TKey,TElement>합니다.The IGrouping<TKey,TElement> objects are yielded in an order based on the order of the elements in source that produced the first key of each IGrouping<TKey,TElement>. 그룹의 요소에 순서 대로 생성 됩니다 source합니다.Elements in a grouping are yielded in the order they appear in source.

하는 경우 comparer 됩니다 null, 기본 같음 비교자를 Default 키를 비교 하는 데 사용 됩니다.If comparer is null, the default equality comparer Default is used to compare keys.

두 개의 키에 따라 같은 것으로 간주 됩니다 comparer, 첫 번째 키가 그룹화 키로 선택 됩니다.If two keys are considered equal according to comparer, the first key is chosen as the key for that grouping.

쿼리 식 구문에는 group by (Visual C#) 또는 Group By Into 절 (Visual Basic)의 호출으로 변환 GroupBy합니다.In query expression syntax, a group by (Visual C#) or Group By Into (Visual Basic) clause translates to an invocation of GroupBy. 자세한 내용 및 사용 예제를 참조 하세요 group 절 하 고 By 절 그룹합니다.For more information and usage examples, see group clause and Group By Clause.

추가 정보

적용 대상