Grant
Grant

Reputation: 11356

Linq2SQL Help Converting row_number and partition

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

Answers (1)

MarcinJuraszek
MarcinJuraszek

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

Related Questions