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