Linq2SQL 帮助转换row_number和分区

本文关键字:number 分区 row 帮助 转换 Linq2SQL | 更新日期: 2023-09-27 18:30:59

有人可以帮我把下面的t-sql转换为c#linq吗?

select
    clientid,
    orderId 
from 
(
    select
        row_number() over (partition by clientid order by purchasedate desc) as rownum,
        clientid,
        id as orderId   
    from ordertraining
) as x where rownum = 1

Linq2SQL 帮助转换row_number和分区

可以使用以下 LINQ 查询获得相同的结果:

from o in Orders
group o by o.clientId into g
select g.OrderByDescending(x => x.purchasedate).FirstOrDefault();

但它不会生成相同的 SQL。它将改用CROSS APPLY