How to select only the records with the highest date in LINQ

前端 未结 5 1148
一向
一向 2020-11-28 03:15

I have a table, \'lasttraces\', with the following fields.

Id, AccountId, Version, DownloadNo, Date

The data looks like this:



        
5条回答
  •  醉酒成梦
    2020-11-28 03:44

    If you just want the last date for each account, you'd use this:

    var q = from n in table
            group n by n.AccountId into g
            select new {AccountId = g.Key, Date = g.Max(t=>t.Date)};
    

    If you want the whole record:

    var q = from n in table
            group n by n.AccountId into g
            select g.OrderByDescending(t=>t.Date).FirstOrDefault();
    

提交回复
热议问题