0

I have a dictionary which holds another dictionary within the value.

i.e.:

Dictionary<string, object> primaryList = new Dictionary<string, object>();
var secondaryList = primaryList.Select(x => x.Value);

I need to get the value as a dictionary in secondaryList. How can i do that?

I'm getting a collection on deserializing the json from the following link:

Deserialize JSON into C# dynamic object?

From the object, i need to parse the value from the primary list to a key value pair.

Community
  • 1
  • 1
NewBie
  • 1,824
  • 8
  • 35
  • 66
  • What type is Value? Is your question how to convert a single value into a Dictionary? – Roy Dictus May 28 '13 at 13:52
  • Can't we convert an object to dictionary? – NewBie May 28 '13 at 13:53
  • Do you know in advance the type of key and the type of value in the contained dictionary? (If you do, why is your outer dictionary not defined to contain that specific type?) – Anthony Pegram May 28 '13 at 13:54
  • Any reason you're using `Dictionary` instead of `ArrayList` or `List<>`? You're not actually using it like a dictionary here. – Tim Rogers May 28 '13 at 13:57
  • Its a json im deserializing using a jsonConvert code from the following link http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object – NewBie May 28 '13 at 13:58
  • In Javascript and many other dynamically typed languages objects and dictionaries are roughly the same thing. Statically typed languages do not work that way, though. You have to define exactly what 'keys' are available for a given type at compile time (before the code runs). Alternatively you could use a ExpandoObject which allows treating it like a dictionary (under the hood that's what's going on anyway). _However_ that being said it's a poor C# style. – Chris Pfohl May 28 '13 at 13:58
  • The code you linked is meant to be simply copy+paste into your project, and then deserialize your json into a poco object. You shouldn't need to deserialize into a dictionary. Can you provide a sample of the json you are deserializing? – Jay May 28 '13 at 14:11

4 Answers4

2

Dictionary<T, V>- is a generic collection, where T or V can be Dictionary too.

Try next:

Dictionary<string, Dictionary<object, object>> 

instead of

Dictionary<string, object>
Eugene K.
  • 85
  • 1
  • 11
  • Also, if you sure that your value is a dictionary, you can cast object to your Dictionary. – Eugene K. May 28 '13 at 14:03
  • If you know the types of objects going into your collections, those types should be used as Geka as demonstrated here. Always avoid using object when possible. – Jay May 28 '13 at 14:05
1

If you know they are all dictionaries then just define your dictionary as

var primaryList = new Dictionary<string, Dictionary<key-type, value-type>>();

Otherwise use OfType() to filter the Values collection.

var secondaryList = primaryList.Select(x => x.Value).OfType<Dictionary<key-type, value-type>>();

Consider using a List<> if you're not actually using Dictionary to do lookups.

JDB
  • 25,172
  • 5
  • 72
  • 123
Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
0

You can use the Cast() extension method to cast objects in a collection to a specific type:

var secondaryList = primaryList.Select(x => x.Value)
                               .Cast<Dictionary<string, object>>();

This will fail with an exception if any of the objects in the collection are not cast-able to Dictionary<string, object>. You can also use OfType<Dictionary<string, object>>() to select only those elements which are of that particular type.

var secondaryList = primaryList.Select(x => x.Value)
                               .OfType<Dictionary<string, object>>();

When selecting a subset of a dictionary, you can use the ToDctionary() extension method.

Dictionary<string, object> l_d = new Dictionary<string, object>();
l_d.Add( "Apple", 1 );
l_d.Add( "Access", 2 );
l_d.Add( "Barber", 3 );
l_d.Add( "Bacon", 4 );

Dictionary<string, object> l_d2 = 
    l_d.Where( x => x.Key.StartsWith( "A" ) )
       .ToDictionary( kvp => kvp.Key, kvp => kvp.Value );
JDB
  • 25,172
  • 5
  • 72
  • 123
0
 Dictionary<string, Dictionary<string, object>> value = new Dictionary<string,   Dictionary<string, object>>();

 Dictionary<string, object> childDictionary = value["SomeValue"];
Kurubaran
  • 8,696
  • 5
  • 43
  • 65