0

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
2
  • Why? Why not use just a view? Why does it absolutely have to be in LINQ2SQL? You'd be surprised at the SQL it generates for something like that. Commented Sep 12, 2013 at 9:07
  • Just preference is all. Commented Sep 12, 2013 at 9:12

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Just want to add, that desired SQL cannot be produced by LINQ2SQL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.