Compartir a través de


IGrouping<TKey,TElement>.Key Propiedad

Definición

Obtiene la clave del objeto IGrouping<TKey,TElement>.

public:
 property TKey Key { TKey get(); };
public TKey Key { get; }
member this.Key : 'Key
Public ReadOnly Property Key As TKey

Valor de propiedad

TKey

Clave de IGrouping<TKey,TElement>.

Ejemplos

En el ejemplo siguiente se muestra cómo usar la Key propiedad para etiquetar cada IGrouping<TKey,TElement> objeto en una secuencia de IGrouping<TKey,TElement> objetos. El GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) método se usa para obtener una secuencia de IGrouping<TKey,TElement> objetos. El foreach en Visual C# o For Each en el bucle de Visual Basic recorre en iteración cada IGrouping<TKey,TElement> objeto, lo que genera su clave y el número de valores que contiene.

// Get a sequence of IGrouping objects.
IEnumerable<IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo>> memberQuery =
    typeof(String).GetMembers().
    GroupBy(member => member.MemberType);

// Output the key of each IGrouping object and the count of values.
foreach (IGrouping<System.Reflection.MemberTypes, System.Reflection.MemberInfo> group in memberQuery)
    Console.WriteLine("(Key) {0} (Member count) {1}", group.Key, group.Count());

// 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
' 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(ByVal 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
    MsgBox(String.Format("(Key) {0} (Member count) {1}", group.Key, group.Count()))
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

Comentarios

La clave de un IGrouping<TKey,TElement> objeto representa el atributo que es común a cada valor de .IGrouping<TKey,TElement>

Se aplica a