c# - 使用 c# 进行聚合分组
问题描述
我有一个看起来像的分组对象
Dictionary<(ushort id, ushort sc), Timepoint[]> timepoints
它看起来像 (1, 2) => [一些字符串时间点]
但我想将其转换为
Dictionary<ushort id, Timepoint[]>
我想聚合那个 sc 并且只有 id。我试过:
test = timepoints.GroupBy(group => group.Key.id).ToDictionary(key => key.Key, value => value);
但我没有运气。
Dictionary<ushort, IGrouping<ushort, KeyValuePair<(ushort id, ushort sc), Timepoint[]>>>
我想我错过了一些东西。
解决方案
您需要使用 aSelectMany
来展平要组合在一起的数组,然后先将它们变成一个数组。
var test = timepoints
.GroupBy(kvp => kvp.Key.id)
.Select(grp => new { grp.Key, Values = grp.SelectMany(x => x.Value).ToArray() })
.ToDictionary(x => x.Key, x => x.Values);
推荐阅读
- elasticsearch - hibernate search indexing annotation 是否支持 Elastic Search 的 'copy_to' 属性
- javascript - Webpack 模式和加载器问题
- excel - 如何使一个单元格等于从另一个单元格返回的单元格地址的值?
- c++ - 找到第 N 个最小的整数
- php - __get 魔术函数和空函数的返回不一致
- php - 如何在没有提交按钮的情况下将表单文本区域数据发送到 txt 文件?
- c# - .Net Core Web API,如果角色或策略匹配,则授权操作
- storyboard - 无法在稳定版 Xcode 上打开情节提要
- blockchain - 如何在基板运行时进行浮点运算
- ajax - 使用 Ajax 调用 WCF 自托管服务:405 方法不允许错误