This documentation is archived and is not being maintained.

IGrouping<TKey, TElement> Interface

Represents a collection of objects that have a common key.

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

public interface IGrouping<TKey, TElement> : IEnumerable<TElement>, 

Type Parameters


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


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

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

The values of an IGrouping<TKey, TElement> are accessed much as the elements of an IEnumerable<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<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<TKey, TElement> object.

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

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

In this example, GroupBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>) is called on the array of MemberInfo objects returned by GetMembers. GroupBy<TSource, TKey>(IEnumerable<TSource>, Func<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<TKey, TElement> object, and the MemberInfo objects that have that key form the IGrouping<TKey, TElement> object's sequence of values.

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

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

// Get an IGrouping object.
IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo> group =
    GroupBy(member => member.MemberType).

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

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

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