我有下面的List<Dictionary<string,string>>
:
List<Dictionary<string,string>> data = new List<Dictionary<string,string>>
{
new Dictionary<string,string> { ["HoleName"] = "first", ["Length"] = "20" },
new Dictionary<string,string> { ["HoleName"] = "second", ["Length"] = "40" },
new Dictionary<string,string> { ["HoleName"] = "first", ["Length"] = "30" }
};
我想把上面的列表按HoleName
分组,得到Length
。我尝试了很多方法,但到目前为止还没有得到任何解决方案。
预期产出:
List<Dictionary<string,string>> data = new List<Dictionary<string,string>>
{
new Dictionary<string,string> { ["HoleName"] = "first", ["Length"] = "30" },
new Dictionary<string,string> { ["HoleName"] = "second", ["Length"] = "40" }
};
2条答案
按热度按时间qltillow1#
您可以使用LINQ。Group by
HoleName
and获取字典的最大Length
值(随着问题的更新)。方法1
1.按
HoleName
分组。1.通过
.Max()
获取最大值(在获取最大值之前,必须将Length
转换为数值类型)。方法2
1.按
HoleName
排序,后跟Length
降序(必须将Length
转换为数值类型)。1.按
HoleName
分组。1.通过
.First()
获取每组的第一个项目。n6lpvg4x2#