Condividi tramite


IGrouping<TKey,TElement>.Key Proprietà

Definizione

Ottiene la chiave del IGrouping<TKey,TElement>.

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

Valore della proprietà

TKey

Chiave del IGrouping<TKey,TElement>.

Esempio

Nell'esempio seguente viene illustrato come usare la Key proprietà per etichettare ogni IGrouping<TKey,TElement> oggetto in una sequenza di IGrouping<TKey,TElement> oggetti. Il GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) metodo viene usato per ottenere una sequenza di IGrouping<TKey,TElement> oggetti. In Visual C# o For Each nel ciclo Visual Basic viene quindi eseguito l'iterazione foreach attraverso ogni IGrouping<TKey,TElement> oggetto, l'output della chiave e il numero di valori contenuti.

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

Commenti

La chiave di un IGrouping<TKey,TElement> oggetto rappresenta l'attributo comune a ogni valore in IGrouping<TKey,TElement>.

Si applica a