Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

ToDictionary Operator

Modified: 2008/06/08 02:54 by samdnp - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The ToDictionary operator creates a Dictionary from a sequence.

Edit

Method Signatures

// 1 - Creates a dictionary (one key & value dictionary entry per element) from a sequence.
public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
    this IEnumerable<TSource> source, 
    Func<TSource, TKey> keySelector)
 
// 2 - Create a dictionary from a sequence using a custom comparer.
public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(
    this IEnumerable<TSource> source, 
    Func<TSource, TKey> keySelector, 
    IEqualityComparer<TKey> comparer)
 
// 3 - Create a dictionary from a sequence of new elements.
public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
    this IEnumerable<TSource> source, 
    Func<TSource, TKey> keySelector, 
    Func<TSource, TElement> elementSelector)
 
// 4 - Create a dictionary from a sequence of new elements using a custom comparer.
public static Dictionary<TKey, TElement> ToDictionary<TSource, TKey, TElement>(
    Ithis Enumerable<TSource> source, 
    Func<TSource, TKey> keySelector, 
    Func<TSource, TElement> elementSelector, 
    IEqualityComparer<TKey> comparer)



Edit

Exceptions

Throws an ArgumentNullException if source, keySelector, or elementSelector are null.


Edit

Pseudo-code

Overload 1
Set comparer to EqualityComparer<TSource>.Default).
Set element selector the be the TSource instance.
Call and return the result of Overload 4 passing in source, keySelector, element selector and comparer.


Overload 2
Set element selector the be the TSource instance.
Call and return the result of Overload 4 passing in source, keySelector, element selector and comparer.


Overload 3
Set comparer to EqualityComparer<TSource>.Default).
Call and return the result of Overload 4 passing in source, keySelector, elementSelector and comparer.


Overload 4
If source is null, throw an ArgumentNullException.
If keySelector is null, throw an ArgumentNullException.
If elementSelector is null, throw an ArgumentNullException.
Create a Dictionary<TKey,TSource> instance, passing in comparer.
Iterate the source sequence.
Call dictionary.Add(keySelector(current element), elementSelector(current element));
Return the dictionary.


Edit

Loop Count

1. The source sequence is enumerated once and elements are added to a dictionary.


Edit

Code Samples

XElement Example
The following code sample shows a LINQ query that returns all XElement objects matching a tag name, and then makes a dictionary out of them. The key in the dictionary is the "key" Attribute on the XElement and the value on the dictionary is the Value property.

var displayDict = (from name in _x.Elements("CategoryName")
     select name).ToDictionary(n => n.Attribute("key").Value, n => n.Value);



ToDictionary Lambda Expression article.

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.