SQL's Rownumber with Linq-to-entities

Posted by mariki on Stack Overflow See other posts from Stack Overflow or by mariki
Published on 2010-05-01T18:06:56Z Indexed on 2010/05/01 18:07 UTC
Read the original article Hit count: 654

I am converting my project to use EF and also want to covert stored procedures into Linq-to-entities queries.

This my SQL query (simple version) that I have trouble to convert:

SELECT 
       CategoryID, Title as CategoryTitle,Description, 
       LastProductTitle,LastProductAddedDate
FROM
(
    SELECT
        C.CategoryID, C.Title,C.Description, C.Section,
        P.Title as LastProductTitle, P.AddedDate as LastProductAddedDate,                                          
        ROW_NUMBER() OVER (PARTITION BY P.CategoryID ORDER BY P.AddedDate DESC) AS Num

    FROM 
         Categories C         
         LEFT JOIN Products P ON P.CategoryID = C.CategoryID
) OuterSelect

WHERE 
     OuterSelect.Num = 1

In words: I want to return all Categories (from Categories table) and title and date of addition of the product (from Products table) that was added last to this category.

How can I achieve this using Entity frame work query?
In most efficient way.

© Stack Overflow or respective owner

Related posts about sql

Related posts about entity-framework