Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
This topic has not yet been rated - Rate this topic

IGrouping<TKey, TElement>.Key Property

Gets the key of the IGrouping<TKey, TElement>.

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)
TKey Key { get; }

Property Value

Type: TKey
The key of the IGrouping<TKey, TElement>.

The key of an IGrouping<TKey, TElement> represents the attribute that is common to each value in the IGrouping<TKey, TElement>.

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

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

.NET Framework

Supported in: 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2013 Microsoft. All rights reserved.