.NET Framework Class Library
EnumerableToDictionaryTSource, TKey Method (IEnumerableTSource, FuncTSource, TKey, IEqualityComparerTKey)

Creates a DictionaryTKey, TValue from an IEnumerableT according to a specified key selector function and key comparer.

Namespace:   System.Linq
Assembly:  System.Core (in System.Core.dll)
Syntax
<[%$TOPIC/bb548554_en-us_VS_110_3_0_0_0_0%]> _
Public Shared Function ToDictionary(Of TSource, TKey) ( _
	source As [%$TOPIC/bb548554_en-us_VS_110_3_0_0_0_1%](Of TSource), _
	keySelector As [%$TOPIC/bb548554_en-us_VS_110_3_0_0_0_2%](Of TSource, TKey), _
	comparer As [%$TOPIC/bb548554_en-us_VS_110_3_0_0_0_3%](Of TKey) _
) As [%$TOPIC/bb548554_en-us_VS_110_3_0_0_0_4%](Of TKey, TSource)
public static [%$TOPIC/bb548554_en-us_VS_110_3_0_1_0_0%]<TKey, TSource> ToDictionary<TSource, TKey>(
	this [%$TOPIC/bb548554_en-us_VS_110_3_0_1_0_1%]<TSource> source,
	[%$TOPIC/bb548554_en-us_VS_110_3_0_1_0_2%]<TSource, TKey> keySelector,
	[%$TOPIC/bb548554_en-us_VS_110_3_0_1_0_3%]<TKey> comparer
)
[[%$TOPIC/bb548554_en-us_VS_110_3_0_2_0_0%]]
public:
generic<typename TSource, typename TKey>
static [%$TOPIC/bb548554_en-us_VS_110_3_0_2_0_1%]<TKey, TSource>^ ToDictionary(
	[%$TOPIC/bb548554_en-us_VS_110_3_0_2_0_2%]<TSource>^ source, 
	[%$TOPIC/bb548554_en-us_VS_110_3_0_2_0_3%]<TSource, TKey>^ keySelector, 
	[%$TOPIC/bb548554_en-us_VS_110_3_0_2_0_4%]<TKey>^ comparer
)
static member ToDictionary : 
        source:[%$TOPIC/bb548554_en-us_VS_110_3_0_3_0_0%]<'TSource> * 
        keySelector:[%$TOPIC/bb548554_en-us_VS_110_3_0_3_0_1%]<'TSource, 'TKey> * 
        comparer:[%$TOPIC/bb548554_en-us_VS_110_3_0_3_0_2%]<'TKey> -> [%$TOPIC/bb548554_en-us_VS_110_3_0_3_0_3%]<'TKey, 'TSource> 

Type Parameters

TSource

The type of the elements of source.

TKey

The type of the keys returned by keySelector.

Parameters

source
Type: System.Collections.GenericIEnumerableTSource

An IEnumerableT to create a DictionaryTKey, TValue from.

keySelector
Type: SystemFuncTSource, TKey

A function to extract a key from each element.

comparer
Type: System.Collections.GenericIEqualityComparerTKey

An IEqualityComparerT to compare keys.

Return Value

Type: System.Collections.GenericDictionaryTKey, TSource
A DictionaryTKey, TValue that contains keys and values.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Exceptions
ExceptionCondition
ArgumentNullException

source or keySelector is .

-or-

keySelector produces a key that is .

ArgumentException

keySelector produces duplicate keys for two elements.

Remarks

If comparer is , the default equality comparer Default is used to compare keys.

Version Information

.NET Framework

Supported in: 4.5.1, 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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, 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.