Key Property
Collapse the table of content
Expand the table of content

IGrouping(Of TKey, TElement).Key Property

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets the key of the IGrouping(Of TKey, TElement).

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

'Declaration
ReadOnly Property Key As TKey

Property Value

Type: TKey
The key of the IGrouping(Of TKey, TElement).

The key of an IGrouping(Of TKey, TElement) represents the attribute that is common to each value in the IGrouping(Of TKey, TElement).

The following example demonstrates how to use the Key property to label each IGrouping(Of TKey, TElement) object in a sequence of IGrouping(Of TKey, TElement) objects. The GroupBy(Of TSource, TKey)(IEnumerable(Of TSource), Func(Of TSource, TKey)) method is used to obtain a sequence of IGrouping(Of TKey, TElement) objects. The foreach in Visual C# or For Each in Visual Basic loop then iterates through each IGrouping(Of TKey, TElement) object, outputting its key and the number of values it contains.


      ' Get a sequence of IGrouping objects.
      Dim memberQuery As  _
      IEnumerable(Of IGrouping(Of System.Reflection.MemberTypes, System.Reflection.MemberInfo)) = _
          Type.GetType("String").GetMembers(). _
          GroupBy(Function(member) member.MemberType)

      ' Output the key of each IGrouping object and the count of values.
      For Each group As  _
      IGrouping(Of System.Reflection.MemberTypes, System.Reflection.MemberInfo) In memberQuery
         outputBlock.Text &= String.Format("(Key) {0} (Member count) {1}", _
                                           group.Key, group.Count()) & vbCrLf
      Next

      ' The output is similar to:
      ' (Key) Method (Member count) 113
      ' (Key) Constructor (Member count) 8
      ' (Key) Property (Member count) 2
      ' (Key) Field (Member count) 1



Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft