简体   繁体   English

LINQ - 如何按日期排序

[英]LINQ - how to sort by date

I have the following table (Records): 我有下表(记录):

RecordID int,
Nickname nvarchar(max),
DateAdded datetime

I need group by max count of records for Nickname. 我需要按昵称的最大记录数分组。 I made it: 我做到了:

        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();

it works 有用

Right now I need to sort it by date too (who first got max records). 现在我也需要按日期排序(谁先获得最大记录)。 I should have a request like it: 我应该有这样的要求:

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

How to do it by LINQ? 如何通过LINQ做到这一点?

I think this is what you want. 我想这就是你想要的。 I've used extension version of Linq which is probably more easier. 我使用了Linq的扩展版本,这可能更容易。 The idea is to calculate MaxCount and MaxDate after GroupBy so you can use it in next OrderBy clauses. 我们的想法是在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();

I think what you're asking for is: 我想你要求的是:

...
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();

When you use group the Key is your grouping but the enumerable is all the records you've grouped so you can still use aggregate functions on them. 当您使用group ,Key是您的分组,但是可枚举是您已分组的所有记录,因此您仍然可以对它们使用聚合函数。

If you want to sort by multiple columns, you can put them in order using the chaining above. 如果要按多列排序,可以使用上面的链接将它们按顺序排列。

    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();

Just add max date for nickname to ordering. 只需将昵称的最大日期添加到订购中。 You also can introduce new range variable for position: 您还可以为位置引入新的范围变量:

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();

Question is already answered here: OrderBy a date field with linq and return ToList() 问题已在这里得到解答: OrderBy带有linq的日期字段并返回ToList()

Add at the end of your users statement 在用户声明的末尾添加

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM