[英]How can i create a nested dictionary in c#
How can i create a nested dictionary which is given below in c# 我如何创建嵌套字典,下面在C#中给出
This is nested json 这是嵌套的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#? 如何在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. 让我知道是否有帮助,我认为这是生成嵌套词典的最有效方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.