0

I have a KeyValuePair<string,object> and the object is also a KeyValuePair. I have been trying to cast the object to a new KeyValuePair so that I can iterate through the values, but so far all attempts have failed. Does anyone have any ideas. Thanks.

var installers = manifestDict.Where(kvp => kvp.Key.Equals("Installers"));
foreach(var i in installers)
{
     var newKvp = i.Value;
     //how to cast this object to a new kvp?
}

enter image description here

user1279156
  • 35
  • 1
  • 5

2 Answers2

1

It looks to me it is another dictionary, no?

So you should cast it to Dictionary<string, object>

var installers = manifestDict.Where(kvp => kvp.Key.Equals("Installers"));
foreach(var i in installers)
{
     var newKvp = (Dictionary<string, object>)i.Value;
     //how to cast this object to a new kvp?
}

btw u can also write it like this if you certain you have that key:

var installers = (Dictionary<string, object>)manifestDict["Installers"];
foreach(var installer in installers)
{
     //This will itenrate all your key value pairs inside installers
     Console.WriteLine(installer.Value);
}
Ziv Weissman
  • 4,400
  • 3
  • 28
  • 61
0

It ended up that I needed to cast to a list first.

 foreach(var i in installers)
 {
      var newKvp = i.Value as List<object>;
      foreach(var z in newKvp)
      {
           var result = (Dictionary<object,object>) z;
      }
 }
user1279156
  • 35
  • 1
  • 5