How can i create a nested dictionary which is given below in c#
This is nested json
{
"blue":[
{
"s":{
[
"store1",
"store2"
],
"availble"
}
},
{
"m":{
[
"store3",
"store4"
],
"not availble"
}
},
{
"l":{
[
"store5"
],
"availble"
}
}
]
},
{
"back":[
{
"xxl":{
[
"store2",
"store4"
],
"not availble"
}
},
{
"m":{
[
"store3",
"store4"
],
"not availble"
}
}
]
}
I tried with below script but it is not working
var variations_hash = new Dictionary<string, List<string>>();
var stores = new[] {"s","m","xl","xxl","xxxl","v"};
var color_trans = new[] {"blue","black"};
foreach(var store in stores){
if (variations_hash.ContainsKey (color_trans)) {
List<String> list;
if (variations_hash.TryGetValue (color_trans, out list)) {
list.Add (store);
}
} else {
variations_hash[color_trans] = new List<string> { store };
}
}
How could i create nested dictionary in c#?
I want nested dictionary in above format
Here is an easy, elegant way to generate the nested dictionary
Dictionary<int, Dictionary<int, string>> dict = things
.GroupBy(thing => thing.Foo)
.ToDictionary(fooGroup => fooGroup.Key,
fooGroup => fooGroup.ToDictionary(thing => thing.Bar,
thing => thing.Baz));
Let me know if that helps, it's the most efficient way to generate a nested dictionary in my opinion.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.