首页 > 解决方案 > 有没有办法将 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[]>>'。存在显式转换。您是否缺少强制转换?”

标签: c#

解决方案


假设您有重复的潜力,您可以执行以下操作:

var items = new List<string>();
var groups = items.Select((item, index) => new {
    Item = item,
    Index = index
}).GroupBy(o => o.Index, o => o.Item);

推荐阅读