C# LINQ group by 然后对结果组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10530806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:12:00  来源:igfitidea点击:

LINQ group by then order groups of result

c#.netlinqentity-framework

提问by Ketchup

I have a table that has the following 3 columns, ID, ShortCode, UploadDate.

我有一个表,其中包含以下 3 列:ID、ShortCode、UploadDate。

I want to use LINQ to group the results by shortcode (and keep all the results) then order those groups and return a list.

我想使用 LINQ 按短代码对结果进行分组(并保留所有结果),然后对这些组进行排序并返回一个列表。

I have the following:

我有以下几点:

 rawData.Provider.CreateQuery<PDFDocument>(qb.rootExperession)
.ToList<PDFDocument>().
GroupBy(b=>b.ShortCode)
.SelectMany(b=>b).ToList<PDFDocument>()

I want to return all results, grouped by ShortCode, the items within each group sorted by UploadDate and the groups sorted so the one that has the most recent document in it first.

我想返回所有结果,按 ShortCode 分组,每个组中按 UploadDate 排序的项目和排序的组,以便首先包含最新文档的组。

Does anyone know if this is even possible?

有谁知道这是否可能?

采纳答案by Rawling

Try

尝试

rawData.Provider.CreateQuery<PDFDocument>(qb.rootExperession)
    .AsEnumerable()
    .OrderByDescending(d => d.UploadDate)
    .GroupBy(d => d.ShortCode)
    .SelectMany(g => g)
    .ToList();

This should

这应该

  • Order the items by upload date (descending so newest first)
  • Then group them by short code - so within each group the items are still sorted
  • The groups are still in descending order, so no need to order again
  • Finally concatenate the results into a single list
  • 按上传日期排序项目(从最新降序)
  • 然后按短代码对它们进行分组 - 因此在每个组中,项目仍然被排序
  • 组仍按降序排列,因此无需再次订购
  • 最后将结果连接成一个列表

If performance is an issue you many be better off doing

如果性能是一个问题,你最好这样做

rawData.Provider.CreateQuery<PDFDocument>(qb.rootExperession)
    .AsEnumerable()
    .GroupBy(d => d.ShortCode)
    .Select(g => g.OrderByDescending(d => d.UploadDate))
    .OrderByDescending(e => e.First().UploadDate)
    .SelectMany(e => e)
    .ToList();

which sorts the contents of each group separately rather than sorting everything first and then grouping.

它对每个组的内容进行单独排序,而不是先对所有内容进行排序,然后再分组。

回答by Falanwe

In fact, you don't want to group by short code, you want to order by them. So the following query should do the trick:

实际上,您不想按短代码分组,而是想按它们排序。所以下面的查询应该可以解决问题:

rawData.Provider.CreateQuery<PDFDocument>(qb.rootExperession)
.ToList()
.OrderBy(b => b.ShortCode)
.ThenBy(b => b.UploadDate)
.ToList()

EditIf you really want to use a GroupBy, you can do so this way:

编辑如果你真的想使用 GroupBy,你可以这样做:

 rawData.Provider.CreateQuery<PDFDocument>(qb.rootExperession)
.ToList()
.GroupBy(b => b.ShortCode)
.SelectMany(grouping => grouping.OrderBy(b => b.UploadDate))
.ToList()

But I discourage it. There is no point creating groups if you do not want groups in the first place!

但我劝阻它。如果您首先不想要群组,则创建群组是没有意义的!

Second edit

第二次编辑

I did not get you wanted the groups ordered by UpdateTime too. It complicates a little the query:

我没有让您也想要由 UpdateTime 订购的组。它使查询复杂化了一点:

rawData.Provider.CreateQuery<PDFDocument>(qb.rootExperession)
.ToList()
.GroupBy(b => b.ShortCode)
.Select(grouping => grouping.OrderByDescending(b => b.UploadDate))
.OrderByDescending(grouping => grouping.First().UploadDate)
.SelectMany(grouping => grouping)
.ToList()