If I have a sql table that contains
ProductID
Price
Date
and I wish to find the latest price for a given date. In Sql I can do something like
`
Select * from (
select ROW_NUMBER() OVER (PARTITION BY ProductID ORDER BY Date DESC) AS row_number,
ProductID, Date, Price from ProductPrices
where date < @targetDate
) tempTable
where row_number = 1
`
Is it possible to do this easily in LinqToSql? My attempts have all ended up issuing on sql command per product?
How about something like this:
It’s possible that calling
Firstwon’t work butFirstOrDefaultwill – someone else found that recently in a similar situation.EDIT: This solution was driven from the title more than from the SQL (which I don’t fully understand). It aims to find the most recent entry for each product. Hope that’s what you were after!