IGrouping<TKey,TElement>.Key Eigenschaft

Definition

Ruft den Schlüssel von IGrouping<TKey,TElement> ab.

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

Eigenschaftswert

TKey

Der Schlüssel des IGrouping<TKey,TElement>.

Beispiele

Im folgenden Beispiel wird veranschaulicht, wie die Key -Eigenschaft verwendet wird, um jedes IGrouping<TKey,TElement> Objekt in einer Sequenz von IGrouping<TKey,TElement> -Objekten zu bezeichnen. Die GroupBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) -Methode wird verwendet, um eine Sequenz von IGrouping<TKey,TElement> -Objekten abzurufen. Die foreach in Visual C# oder For Each in Visual Basic-Schleife durchlaufen dann jedes IGrouping<TKey,TElement> Objekt und gibt seinen Schlüssel und die Anzahl der darin enthaltenen Werte aus.

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

Hinweise

Der Schlüssel eines IGrouping<TKey,TElement> stellt das Attribut dar, das jedem Wert in der IGrouping<TKey,TElement>gemeinsam ist.

Gilt für: