LINQ不包含'Key'无扩展方法'键'接受类型的第一个参数
本文关键字:类型 参数 第一个 扩展 包含 Key LINQ 方法 | 更新日期: 2023-09-27 18:17:48
我是LINQ的新手。我有这段代码
l.realtimed_graph = new[] { l.rtl[0].realtimed, l.rtl[1].realtimed, l.rtl[2].realtimed }.SelectMany(p => p).GroupBy(p => p.Key)
.ToDictionary(g => g.Key, g => g.Max(p => p.Value));
现在,因为我需要使用未知数字的字典,所以我将代码修改为
l.realtimed_graph = new[] { l.rtl.ToArray() }.SelectMany(p => p).GroupBy(p => p.Key)
.ToDictionary(g => g.Key, g => g.Max(p => p.Value));
和get错误"不包含'Key'的定义"和扩展方法'Key'不接受类型为。的第一个参数
看起来你想:
l.realtimed_graph = l.rtl.SelectMany(p => p.realtimed).GroupBy(p => p.Key)
.ToDictionary(g => g.Key, g => g.Max(p => p.Value));
因为我不知道你的类的类型和属性,我创建了一个' Tuple'的例子:
var dict = new List<IEnumerable<Tuple<string, int>>>
{
new Tuple<string, int>[] {
new Tuple<string, int>("a", 1)
},
new Tuple<string, int>[] {
new Tuple<string, int>("b", 3),
new Tuple<string, int>("a", 2),
new Tuple<string, int>("a", 3),
},
new Tuple<string, int>[] {
new Tuple<string, int>("b", 2),
new Tuple<string, int>("c", 5),
new Tuple<string, int>("b", 3),
new Tuple<string, int>("c", 2),
new Tuple<string, int>("c", 2),
new Tuple<string, int>("a", 3)
}
};
var grouped = dict.SelectMany(p => p).GroupBy(p => p.Item1);
// results in list of int values grouped by string value
// list of IEnumerable<IGrouping<string, Tuple<string, int>>>
// a - 1, 2, 3, 3
// b - 3, 2, 3
// c - 5, 2, 2
var dict = grouped.ToDictionary(g => g.Key, g => g.Max(p => p.Item2));
// results in simple dictionary: a - 3, b - 3, c - 5