我试图将此sql语句转换为Linq格式,但没有成功。而不是显示所有的数据,我想从每个类别显示十个项目。任何帮助都太好了。
select *
from (
select *,
row_number() over(partition by T.category order by T.id desc) as rn
from clothes as T
) as T
where T.rn <= 10;
我对linq的尝试如下;
var query= from (subquery) as DB in dataContext.table where DB.rn <=10;
select DB;
var subquery = row_number() over(partition by DB.id order by DB.category desc) as rn from DB in dataContext.table as DB
select DB;
使用GroupBy
, Take
和SelectMany
应该可以达到目的
var result = dataContext.table.GroupBy(m => m.category)
.SelectMany(g => g.OrderByDescending(x => x.Id).Take(10));