linq.ToDictionary

Applies two selectors on an array to create an object representing a dictionary

Overloads

  • Object ToDictionary (keySelector)
  • Object ToDictionary (keySelector, valueSelector)

Object ToDictionary (keySelector)

Applies keySelector and assumes the values associated with the keys are the objects themselves

Example

var myArray = [{x: "a", y: 42}, {x: "b", y: 99}, {x: "c", y: "Hello"}];

//Returns {"a": {x: "a", y: 42}, "b": {x: "b", y: 99}, "c": {x: "c", y: "Hello"} }
var result = myArray.ToDictionary("o => o.x");

// {x: "a", y: 42}
result["a"]

// {x: "b", y: 99}
result["b"]

// {x: "c", y: "Hello"}
result["c"]

Object ToDictionary (keySelector, valueSelector)

Applies keySelector and valueSelector to generate an object containing the selected values

Example

var myArray = [{x: "a", y: 42}, {x: "b", y: 99}, {x: "c", y: "Hello"}];

//Returns {"a": 42, "b": 99, "c": "Hello"};
var result = myArray.ToDictionary("o => o.x", "o => o.y");

// 42
result["a"]

// 99
result["b"]

//"Hello"
result["c"]

Last edited Sep 25, 2011 at 12:14 AM by mlorbetske, version 1

Comments

No comments yet.