【发布时间】:2021-10-26 08:11:27
【问题描述】:
我有一个字典列表如下:
List<Dictionary<string, object>> itemList = new List<Dictionary<string, object>>();
itemList.Add(new Dictionary<string, object>() {
{ "Item", "001" },
{ "Category", "A" },
{ "Quantity", 5 }
});
itemList.Add(new Dictionary<string, object>() {
{ "Item", "002" },
{ "Category", "B" },
{ "Quantity", 8 }
});
itemList.Add(new Dictionary<string, object>() {
{ "Item", "001" },
{ "Category", "A" },
{ "Quantity", 6 }
});
我将如何编写我的 Linq 查询,以便获得如下结果
Output (list of dictionaries):
{ "Item" = "001", "Category" = "A", "Quantity" = 11},
{ "Item" = "002", "Category" = "B", "Quantity" = 8}
【问题讨论】:
-
我猜原始数据来自sql什么的?如果是这种情况,我建议将数据反序列化为对象而不是字典。
-
@LeisenChang 是的,如果将其反序列化为对象,编写linq查询将变得更加简单。但是我们的系统允许用户动态添加新字段而无需重新编译代码,所以字典类型是我找到的最好的方式(暂时)......
标签: c# list linq dictionary