[英]LINQ to SQL GroupBy, Take and OrderBy
I have a table with records 我有一张有记录的桌子
| ID | FKID | Date | Username |
--------------------------------------------------------------
| A | a1 | 01/01/01 | Joe |
| B | a1 | 02/01/01 | Joe |
| C | b1 | 03/01/01 | Joe |
| D | b1 | 04/01/01 | Joe |
| E | c2 | 05/01/01 | Joe |
| F | c2 | 06/01/01 | Joe |
I want to select 3 records with distinct FKID
ordered with the latest date first belonging to Joe
. 我想选择3个具有不同
FKID
记录, FKID
记录的最新日期首先属于Joe
。
| F | c2 | 06/01/01 | Joe |
| D | b1 | 04/01/01 | Joe |
| B | a1 | 02/01/01 | Joe |
I can achieve this for one record: 我可以达成一项记录:
Table.Where(a => a.Username.Equals("Joe"))
.GroupBy(a => a.FKID)
.Select(g => g.FirstOrDefault())
.ToList<Record>();
but cannot get Take()
and OrderByDecending()
to work correctly. 但无法使
Take()
和OrderByDecending()
正常工作。
You need OrderByDescending
:- 您需要
OrderByDescending
:-
Table.Where(a => a.Username.Equals("Joe"))
.GroupBy(a => a.FKID)
.Select(g => g.OrderByDescending(x => x.Date).FirstOrDefault())
.Take(3)
.ToList<Record>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.