如何获取字典列表中与同一键关联的所有值?
How to get all values associated with the same key in a list of dictionaries?
我有一个 Class 看起来像这样
public class VegetablesPriceNode
{
public string Country{get;set;}
public Dictionary<string,string> VegetableDictionary{get;set;}
}
然后我以列表的形式获取每个 Individual VegetablesPriceNode
List<VegetablesPriceNode> = //I get this object after parsing.
所有 VegetableDictionary 中都有相同的键,但针对不同的国家/地区,我需要将其水平显示。
Veggies Afghanistan. Bahrain
-------------------------------------------------------------------------
Onions | .00 | .00 ....
Potatoes| .00 | .50 ....
.......
public static void Main()
{
// I get a List<VegetablesPriceNode> from some place
// Need to extract all values for the same key from all dictionaries
// I tried to extract all keys first using
var = vegetablesPriceNodeList.SelectMany(x => x.VegetableDictionary.Select(x => x.Key)).ToHashSet();
// I chose HashSet because all keys were getting appended and there is no unique.
}
如何遍历所有国家/地区的所有词典来提取相同的关键信息?
例如,我想要所有国家/地区的所有洋葱价格。所以循环遍历所有字典,以 Onions 为键并获取它们的值?
上述先提取键然后获取该键的所有值的方法是否理想?或者有更好的解决方案吗?
因为我使用 DataTable 来存储这些信息
DataTable 使用对象[]
我得到的对象是 List
预计:
看起来像
的字符串值列表
"Onions",".00",".00"
示例数据:
[{"Country":"Afghanistan","VegetableDictionary":{"Onions":".00","Potatoes":".40"}},{"Country":"Bahrain","VegetableDictionary":{"Onions":".00","Potatoes":".40"}}]
我不知道你想要的输出是什么。
我相信类似的东西:
[["Onions",".00",".00"],["Potatoes",".40",".40"]]
我做了一个例子,希望它对你的用例有用:
https://dotnetfiddle.net/RPFZen
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
public class VegetablesPriceNode
{
public string Country { get; set; }
public Dictionary<string, string> VegetableDictionary { get; set; }
}
public class Program
{
public static void Main()
{
var json = "[{\"Country\":\"Afghanistan\",\"VegetableDictionary\":{\"Onions\":\".00\",\"Potatoes\":\".40\"}},{\"Country\":\"Bahrain\",\"VegetableDictionary\":{\"Onions\":\".00\",\"Potatoes\":\".40\"}}]";
var vegetablesPriceNodeList = JsonConvert.DeserializeObject<List<VegetablesPriceNode>>(json);
var obj = vegetablesPriceNodeList
.SelectMany(x =>
x.VegetableDictionary
.Select(x => x.Key)
)
.Distinct()
.Select((k) =>
vegetablesPriceNodeList
.Select(v => v.VegetableDictionary[k])
.Prepend(k)
.ToArray()
).ToArray();
var output = JsonConvert.SerializeObject(obj);
Console.WriteLine(output);
}
}
我有一个 Class 看起来像这样
public class VegetablesPriceNode
{
public string Country{get;set;}
public Dictionary<string,string> VegetableDictionary{get;set;}
}
然后我以列表的形式获取每个 Individual VegetablesPriceNode
List<VegetablesPriceNode> = //I get this object after parsing.
所有 VegetableDictionary 中都有相同的键,但针对不同的国家/地区,我需要将其水平显示。
Veggies Afghanistan. Bahrain
-------------------------------------------------------------------------
Onions | .00 | .00 ....
Potatoes| .00 | .50 ....
.......
public static void Main()
{
// I get a List<VegetablesPriceNode> from some place
// Need to extract all values for the same key from all dictionaries
// I tried to extract all keys first using
var = vegetablesPriceNodeList.SelectMany(x => x.VegetableDictionary.Select(x => x.Key)).ToHashSet();
// I chose HashSet because all keys were getting appended and there is no unique.
}
如何遍历所有国家/地区的所有词典来提取相同的关键信息? 例如,我想要所有国家/地区的所有洋葱价格。所以循环遍历所有字典,以 Onions 为键并获取它们的值?
上述先提取键然后获取该键的所有值的方法是否理想?或者有更好的解决方案吗?
因为我使用 DataTable 来存储这些信息 DataTable 使用对象[] 我得到的对象是 List
预计: 看起来像
的字符串值列表"Onions",".00",".00"
示例数据:
[{"Country":"Afghanistan","VegetableDictionary":{"Onions":".00","Potatoes":".40"}},{"Country":"Bahrain","VegetableDictionary":{"Onions":".00","Potatoes":".40"}}]
我不知道你想要的输出是什么。
我相信类似的东西:
[["Onions",".00",".00"],["Potatoes",".40",".40"]]
我做了一个例子,希望它对你的用例有用:
https://dotnetfiddle.net/RPFZen
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
public class VegetablesPriceNode
{
public string Country { get; set; }
public Dictionary<string, string> VegetableDictionary { get; set; }
}
public class Program
{
public static void Main()
{
var json = "[{\"Country\":\"Afghanistan\",\"VegetableDictionary\":{\"Onions\":\".00\",\"Potatoes\":\".40\"}},{\"Country\":\"Bahrain\",\"VegetableDictionary\":{\"Onions\":\".00\",\"Potatoes\":\".40\"}}]";
var vegetablesPriceNodeList = JsonConvert.DeserializeObject<List<VegetablesPriceNode>>(json);
var obj = vegetablesPriceNodeList
.SelectMany(x =>
x.VegetableDictionary
.Select(x => x.Key)
)
.Distinct()
.Select((k) =>
vegetablesPriceNodeList
.Select(v => v.VegetableDictionary[k])
.Prepend(k)
.ToArray()
).ToArray();
var output = JsonConvert.SerializeObject(obj);
Console.WriteLine(output);
}
}