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

Creates a Dictionary(Of TKey, TValue) from an IEnumerable(Of T) according to a specified key selector function and key comparer.

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)

<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)
Dim source As IEnumerable(Of TSource)
Dim keySelector As Func(Of TSource, TKey)
Dim comparer As IEqualityComparer(Of TKey)
Dim returnValue As Dictionary(Of TKey, TSource)

returnValue = source.ToDictionary(keySelector, _

Type Parameters


The type of the elements of source.


The type of the keys returned by keySelector.


Type: System.Collections.Generic.IEnumerable(Of TSource)

An IEnumerable(Of T) to create a Dictionary(Of TKey, TValue) from.

Type: System.Func(Of TSource, TKey)

A function to extract a key from each element.

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).


source or keySelector is Nothing.


keySelector produces a key that is Nothing.


keySelector produces duplicate keys for two elements.

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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5

.NET Compact Framework

Supported in: 3.5

XNA Framework

Supported in: 3.0