Enumerable.GroupBy(Of TSource, TKey, TElement) Method (IEnumerable(Of TSource), Func(Of TSource, TKey), Func(Of TSource, TElement))


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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.

Namespace:   System.Linq
Assembly:  System.Core (in System.Core.dll)

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


Type: System.Collections.Generic.IEnumerable(Of TSource)

An IEnumerable(Of T) whose elements to group.

Type: System.Func(Of TSource, TKey)

A function to extract the key for each element.

Type: System.Func(Of TSource, TElement)

A function to map each source element to an element in the IGrouping(Of TKey, TElement).

Return Value

Type: System.Collections.Generic.IEnumerable(Of IGrouping(Of TKey, TElement))

An IEnumerable<IGrouping<TKey, TElement>> in C# or IEnumerable(Of IGrouping(Of TKey, TElement)) in Visual Basic where each IGrouping(Of TKey, TElement) object contains a collection of objects of type TElement and a key.

Type Parameters


The type of the elements of source.


The type of the key returned by keySelector.


The type of the elements in the IGrouping(Of TKey, TElement).

Exception Condition

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

The GroupBy(Of TSource, TKey, TElement)(IEnumerable(Of TSource), Func(Of TSource, TKey), Func(Of TSource, TElement)) method returns a collection of IGrouping(Of TKey, TElement) objects, one for each distinct key that was encountered. An IGrouping(Of TKey, TElement) is an IEnumerable(Of T) that also has a key associated with its elements.

The IGrouping(Of 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(Of TKey, TElement). Elements in a grouping are yielded in the order that the elements that produced them appear in source.

The default equality comparer Default is used to compare keys.

The following code example demonstrates how to use GroupBy(Of TSource, TKey, TElement)(IEnumerable(Of TSource), Func(Of TSource, TKey), Func(Of TSource, TElement)) to group the elements of a sequence.

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.
        ' Iterate over each value in the IGrouping and print the value.
        For Each name As String In petGroup
            output.AppendLine("  " & name)

    ' Display the output.
End Sub

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

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.

    Dim query =
From pet In pets
Group pet.Name By Age = pet.Age Into ageGroup = Group

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(Of TSource, TKey, TElement)(IEnumerable(Of TSource), Func(Of TSource, TKey), Func(Of TSource, TElement)) method.

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top