Reputation: 11356
Can someone please help me convert the following t-sql into 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
Upvotes: 0
Views: 105
Reputation: 125640
You can get the same results with following LINQ query:
from o in Orders
group o by o.clientId into g
select g.OrderByDescending(x => x.purchasedate).FirstOrDefault();
but it will not generate the same SQL. It will use CROSS APPLY
instead.
Upvotes: 1