This documentation is archived and is not being maintained.
Enumerable.ToDictionary(Of TSource, TKey) Method (IEnumerable(Of TSource), Func(Of TSource, TKey), IEqualityComparer(Of TKey))
Visual Studio 2010
Creates a Dictionary(Of TKey, TValue) from an IEnumerable(Of T) according to a specified key selector function and key comparer.
Assembly: System.Core (in System.Core.dll)
'Declaration <ExtensionAttribute> _ Public Shared Function ToDictionary(Of TSource, TKey) ( _ source As IEnumerable(Of TSource), _ keySelector As Func(Of TSource, TKey), _ comparer As IEqualityComparer(Of TKey) _ ) As Dictionary(Of 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.Generic.IEnumerable(Of TSource)
An IEnumerable(Of T) to create a Dictionary(Of TKey, TValue) from.
- keySelector
- Type: System.Func(Of TSource, TKey)
A function to extract a key from each element.
- comparer
- Type: System.Collections.Generic.IEqualityComparer(Of TKey)
An IEqualityComparer(Of T) to compare keys.
Return Value
Type: System.Collections.Generic.Dictionary(Of TKey, TSource)A Dictionary(Of TKey, 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 IEnumerable(Of TSource). 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).| Exception | Condition |
|---|---|
| ArgumentNullException | source or keySelector is Nothing. -or- keySelector produces a key that is Nothing. |
| ArgumentException | keySelector produces duplicate keys for two elements. |
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: