如何将SQL转换为Linq



我试图将此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, TakeSelectMany应该可以达到目的

var result = dataContext.table.GroupBy(m => m.category)
             .SelectMany(g => g.OrderByDescending(x => x.Id).Take(10));

相关内容

  • 没有找到相关文章

最新更新