41

I have to convert System.Collections.Generic.IDictionary<string, decimal> to System.Collections.Generic.Dictionary<string, decimal>, and i can't. I tried the ToDictionary method and can't specify right arguments.

I've tried the following:

// my dictionary is PlannedSurfaces (of type IDictionary<string, decimal>)
blabla.ToDictionary<string, decimal>(localConstruction.PlannedSurfaces) 
Isak Savo
  • 34,957
  • 11
  • 60
  • 92
croisharp
  • 1,926
  • 5
  • 25
  • 40

3 Answers3

82

var newDict = new Dictionary<string, decimal>(oldDictionary)

Ray
  • 45,695
  • 27
  • 126
  • 169
33

or in linq

var castedDico = dictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
Tomasz Jaskuλa
  • 15,723
  • 5
  • 46
  • 73
0
var newDictionary = (IDictionary<string, boolean>)ex.Data;

IDictionary can be casted as a Dictionary.

Note that if dictionary is empty/null, it will throw exception.

Kishan Vaishnav
  • 2,273
  • 1
  • 17
  • 45