【问题标题】:LINQ - how to sort by dateLINQ - 如何按日期排序
【发布时间】:2014-05-23 17:53:51
【问题描述】:

我有下表(记录):

RecordID int,
Nickname nvarchar(max),
DateAdded datetime

我需要按昵称的最大记录数分组。我做到了:

        var users = (from i in db.Records
                     where i.Form.CompetitionID == cID
                     group i by i.Nickname into g
                     orderby g.Count() descending
                     select new TopUserModel()
                     {
                         Nickname = g.Key,
                         Position = g.Count()
                     }).Take(100).ToList();

有效

现在我也需要按日期对其进行排序(谁首先获得了最大记录)。 我应该有这样的要求:

select Nickname, Count(*) as Result, MAX(DateAdded) as MDate from Records group by Nickname order by Result Desc, MDate Asc

LINQ 怎么做?

【问题讨论】:

  • 您的 SQL 不起作用,因为您不能在 ORDER BY 中使用 MDate,因为它未在 GROUP BY 中指定

标签: c# linq


【解决方案1】:

我想这就是你想要的。我使用了 Linq 的扩展版本,这可能更容易。这个想法是在 GroupBy 之后计算 MaxCount 和 MaxDate,以便您可以在下一个 OrderBy 子句中使用它。

db.Records
.Where(i => i.Form.CompetitionID == cID)
.GroupBy(i => i.Nickname)
.Select(g => new { MaxCount = g.Count(), MaxDate = g.Max(i => i.DateAdded), Nickname = g.Key})
.OrderByDescending(gx => gx.MaxCount)
.ThenByDescending(gx => gx.MaxDate)
.Select(gx => new TopUserModel()
{
     Nickname = gx.Nickname,
     Position = gx.MaxCount
}).Take(100).ToList();

【讨论】:

  • 再更正 - 应该是 ThenBy(gx => gx.MaxDate) 而不是 .ThenByDescending(gx => gx.MaxDate)
【解决方案2】:

我想你要的是:

...
select new TopUserModel()
{
    Nickname = g.Key,
    Position = g.Count()
    Date = g.Max(r => r.DateAdded)
}).Take(100).OrderByDescending(t => t.Position).ThenBy(t => t.Date).ToList();

当您使用 group 时,键是您的分组,但可枚举的是您分组的所有记录,因此您仍然可以对它们使用聚合函数。

如果要按多列排序,可以使用上面的链接将它们按顺序排列。

【讨论】:

  • 既然问题说明要对结果进行排序,那么就应该根据Date属性添加排序逻辑
  • 这将丢失之前完成的原始排序。我相信 OP 想要两列排序(见我的回答)。
  • 他将 .Count() 转换为我首先订购的 .Position,然后是 .Date。
【解决方案3】:
    var users = (from i in db.Records
                         where i.Form.CompetitionID == cID
                         group i by new {i.Nickname} into g
                         orderby g.Count() descending, 
                         select new TopUserModel()
    {
        Nickname = g.Key,
        Position = g.Count()
        Date = g.Max(r => r.DateAdded)
    }).Take(100

).OrderBy(c => c.Date).ToList();

【讨论】:

  • 当我添加答案时,@Tristan Warner-Smith 的答案不包含按日期排列的顺序。否则我不会添加答案
【解决方案4】:

只需将昵称的最大日期添加到订购中。您还可以为位置引入新的范围变量:

var users = (from r in db.Records
             where r.Form.CompetitionID == cID
             group r by r.Nickname into g
             let position = g.Count()
             orderby position descending, g.Max(r => r.DateAdded)
             select new TopUserModel {
                 Nickname = g.Key,
                 Position = position
             }).Take(100).ToList();

【讨论】:

    【解决方案5】:

    这里已经回答了问题:OrderBy a date field with linq and return ToList()

    在用户声明的末尾添加

        .OrderBy(e => e.Date).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      相关资源
      最近更新 更多