c# - 有没有办法将 GroupBy 分成一组?
问题描述
我已经把自己发展到了一个角落,指望一个只需要处理分组记录的功能。现在它的任务是处理个人记录。我不想仅仅为了单独处理记录而去重构我的代码。有没有办法使用 .GroupBy 让 C# List 组每条记录自己?
List<string[]> CsvData = new List<string[]>() { };
IEnumerable<IGrouping<string, string[]>> GroupedCsvData = CsvData.GroupBy(x => x);
这会产生以下错误:“无法将类型隐式转换'IEnumerable<IGrouping<string[], string[]>>'
为'IEnumerable<IGrouping<string, string[]>>'
。存在显式转换。您是否缺少强制转换?”
解决方案
假设您有重复的潜力,您可以执行以下操作:
var items = new List<string>();
var groups = items.Select((item, index) => new {
Item = item,
Index = index
}).GroupBy(o => o.Index, o => o.Item);
推荐阅读
- combinatorics - 如何在 R 中进行最佳组合选择
- javascript - 在JavaScript(NodeJs)中调用对象内部的函数名称时有所不同
- mysql - 如何从 Google Cloud 的 VM 转储和下载 mysql
- javascript - 异步函数在另一个函数中调用时不等待 Promise
- mysql - SELECT 的奇怪回答行为
- java - 通过 Payara 4 诊断与 Oracle 的 XA 连接
- xaml - XAML:ImageBrush 水平平铺
- angular - 我可以将一个应用程序的组件用于另一个应用程序吗?
- mysql - 具有非 utf8 编码的 MySQL SUBSTRING()
- vba - 将所有锚点移动到浮动图像顶部的 Word VBA 宏