Partilhar via


IGrouping<TKey,TElement>.Key Propriedade

Definição

Obtém a chave do IGrouping<TKey,TElement>.

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

Valor da propriedade

TKey

A chave do IGrouping<TKey,TElement>.

Exemplos

O exemplo a seguir demonstra como usar a Key propriedade para rotular cada IGrouping<TKey,TElement> objeto em uma sequência de IGrouping<TKey,TElement> objetos. O GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) método é usado para obter uma sequência de IGrouping<TKey,TElement> objetos. O foreach no Visual C# ou For Each no loop do Visual Basic itera em cada IGrouping<TKey,TElement> objeto, gerando sua chave e o número de valores que ele contém.

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

Comentários

A chave de um IGrouping<TKey,TElement> representa o atributo que é comum a cada valor no IGrouping<TKey,TElement>.

Aplica-se a