【问题标题】:list<String[]>.groupby / orderby Linqlist<String[]>.groupby / orderby Linq
【发布时间】:2013-05-28 00:00:15
【问题描述】:

我的代码:

List<string[]> records=new List<string[]>();
.....
....
#Now i has list in records
var sortedRecords = records
                    .OrderBy(o => o[0])
                    .ThenBy(t=>t[3])
                    .ToList();
.....

我想按 [0] 元素分组,按每个组中的 [3] 元素排序。我没有得到正确的输出。怎么了?

【问题讨论】:

  • 那么你期望输出是什么,你得到了什么?没有足够的细节可以解决。
  • 我编辑了我最初声明的记录
  • 我想按 [0] 元素分组并按每个组中的 [3] 元素排序

标签: c# linq group-by


【解决方案1】:

您可以使用 GroupBy o[0] 拳头,然后它会给您分组结果。然后每个组您可以通过d[3] 对项目进行排序,由于分组有序分组列表您可以使用SelectMany 方法将其展平回您期望的列表。

var sortedRecords = records
                    .GroupBy(o => o[0])
                    .Select(g => g.OrderBy(d => d[3]))
                    .SelectMany(g=>g)
                    .ToList();

【讨论】:

  • @Damith 你能解释一下吗?
猜你喜欢
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多