This documentation is archived and is not being maintained.

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

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

<ExtensionAttribute> _
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))
Dim source As IEnumerable(Of TSource)
Dim keySelector As Func(Of TSource, TKey)
Dim elementSelector As Func(Of TSource, TElement)
Dim returnValue As IEnumerable(Of IGrouping(Of TKey, TElement))

returnValue = source.GroupBy(keySelector, _

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


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.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable(Of TSource). When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).


source or keySelector or elementSelector is Nothing.

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.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5

.NET Compact Framework

Supported in: 3.5

XNA Framework

Supported in: 3.0