IGrouping<TKey,TElement> Interface

Definition

Represents a collection of objects that have a common key.

generic <typename TKey, typename TElement>
public interface class IGrouping : System::Collections::Generic::IEnumerable<TElement>
public interface IGrouping<out TKey,out TElement> : System.Collections.Generic.IEnumerable<out TElement>
type IGrouping<'Key, 'Element> = interface
    interface seq<'Element>
    interface IEnumerable
Public Interface IGrouping(Of Out TKey, Out TElement)
Implements IEnumerable(Of Out TElement)

Type Parameters

TKey

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

TElement

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

Implements

Examples

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

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

Remarks

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

Properties

Key

Gets the key of the IGrouping<TKey,TElement>.

Methods

GetEnumerator()

Returns an enumerator that iterates through a collection.

(Inherited from IEnumerable)

Extension Methods

CopyToDataTable<T>(IEnumerable<T>)

Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>)

Returns a collection of elements that contains the ancestors of every node in the source collection.

Ancestors<T>(IEnumerable<T>, XName)

Returns a filtered collection of elements that contains the ancestors of every node in the source collection. Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>)

Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>)

Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Descendants<T>(IEnumerable<T>, XName)

Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection.

Elements<T>(IEnumerable<T>)

Returns a collection of the child elements of every element and document in the source collection.

Elements<T>(IEnumerable<T>, XName)

Returns a filtered collection of the child elements of every element and document in the source collection. Only elements that have a matching XName are included in the collection.

InDocumentOrder<T>(IEnumerable<T>)

Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

Nodes<T>(IEnumerable<T>)

Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>)

Removes every node in the source collection from its parent node.

Applies to