.NET Framework Class Library
IGrouping<(Of <(TKey, TElement>)>) Interface

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Represents a collection of objects that have a common key.

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

Visual Basic (Declaration)
Public Interface IGrouping(Of Out TKey, Out TElement) _
    Inherits IEnumerable(Of TElement), IEnumerable
Visual Basic (Usage)
Dim instance As IGrouping(Of Out TKey, Out TElement)
C#
public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>, 
    IEnumerable
Visual C++
generic<typename TKey, typename TElement>
public interface class IGrouping : IEnumerable<TElement>, 
    IEnumerable
F#
type IGrouping<'TKey, 'TElement> =  
    interface
        interface IEnumerable<'TElement>
        interface IEnumerable
    end

Type Parameters

out Out out out TKey

The type of the key of the IGrouping<(Of <(TKey, TElement>)>).

This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.

out Out out out TElement

The type of the values in the IGrouping<(Of <(TKey, TElement>)>).

Remarks

An IGrouping<(Of <(TKey, TElement>)>) is an IEnumerable<(Of <(T>)>) that additionally has a key. The key represents the attribute that is common to each value in the IGrouping<(Of <(TKey, TElement>)>).

The values of an IGrouping<(Of <(TKey, TElement>)>) are accessed much as the elements of an IEnumerable<(Of <(T>)>) are accessed. For example, you can access the values by using a foreach in Visual C# or For Each in Visual Basic loop to iterate through the IGrouping<(Of <(TKey, TElement>)>) object. The Example section contains a code example that shows you how to access both the key and the values of an IGrouping<(Of <(TKey, TElement>)>) object.

The IGrouping<(Of <(TKey, TElement>)>) type is used by the GroupBy()()() standard query operator methods, which return a sequence of elements of type IGrouping<(Of <(TKey, TElement>)>).

Examples

The following example demonstrates how to work with an IGrouping<(Of <(TKey, TElement>)>) object.

In this example, GroupBy<(Of <(TSource, TKey>)>)(IEnumerable<(Of <(TSource>)>), Func<(Of <(TSource, TKey>)>)) is called on the array of MemberInfo objects returned by GetMembers. GroupBy<(Of <(TSource, TKey>)>)(IEnumerable<(Of <(TSource>)>), Func<(Of <(TSource, TKey>)>)) groups the objects based on the value of their MemberType property. Each unique value for MemberType in the array of MemberInfo objects becomes a key for a new IGrouping<(Of <(TKey, TElement>)>) object, and the MemberInfo objects that have that key form the IGrouping<(Of <(TKey, TElement>)>) object's sequence of values.

Finally, the First<(Of <(TSource>)>) method is called on the sequence of IGrouping<(Of <(TKey, TElement>)>) objects to obtain just the first IGrouping<(Of <(TKey, TElement>)>) object.

The example then outputs the key of the IGrouping<(Of <(TKey, TElement>)>) object and the Name property of each value in the IGrouping<(Of <(TKey, TElement>)>) object's sequence of values. Notice that to access an IGrouping<(Of <(TKey, TElement>)>) object's sequence of values, you simply use the IGrouping<(Of <(TKey, TElement>)>) variable itself.

Visual Basic
        ' Get an IGrouping object.
        Dim group As IGrouping(Of System.Reflection.MemberTypes, System.Reflection.MemberInfo) = _
            Type.GetType("String").GetMembers(). _
            GroupBy(Function(ByVal member) member.MemberType). _
            First()

        ' Output the key of the IGrouping, then iterate
        ' through each value in the sequence of values
        ' of the IGrouping and output its Name property.
        MsgBox(String.Format("\nValues that have the key '{0}':", group.Key))
        For Each mi As System.Reflection.MemberInfo In group
            MsgBox(mi.Name)
        Next

        ' The output is similar to:

        ' Values that have the key 'Method':
        ' get_Chars
        ' get_Length
        ' IndexOf
        ' IndexOfAny
        ' LastIndexOf
        ' LastIndexOfAny
        ' Insert
        ' Replace
        ' Replace
        ' Remove
        ' Join
        ' Join
        ' Equals
        ' Equals
        ' Equals
        ' ...

C#
            // Get an IGrouping object.
            IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo> group =
                typeof(String).GetMembers().
                GroupBy(member => member.MemberType).
                First();

            // Output the key of the IGrouping, then iterate
            // through each value in the sequence of values
            // of the IGrouping and output its Name property.
            Console.WriteLine("\nValues that have the key '{0}':", group.Key);
            foreach (System.Reflection.MemberInfo mi in group)
                Console.WriteLine(mi.Name);

            // The output is similar to:

            // Values that have the key 'Method':
            // get_Chars
            // get_Length
            // IndexOf
            // IndexOfAny
            // LastIndexOf
            // LastIndexOfAny
            // Insert
            // Replace
            // Replace
            // Remove
            // Join
            // Join
            // Equals
            // Equals
            // Equals
            // ...

Platforms

Windows 7, Windows Vista, Windows XP SP2, 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.
Version Information

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET Compact Framework

Supported in: 3.5

XNA Framework

Supported in: 3.0
See Also

Reference

Page view tracker