How would I convert this SQL query to Linq?
Declare @Date Date
select @Date = Max (InsertedDate) from Rates
SELECT * FROM Rates where CAST(InsertedDate AS Date) =CAST(@Date AS Date) ORDER BY InsertedDate DESC
How would I convert this SQL query to Linq?
Declare @Date Date
select @Date = Max (InsertedDate) from Rates
SELECT * FROM Rates where CAST(InsertedDate AS Date) =CAST(@Date AS Date) ORDER BY InsertedDate DESC
Your Sql Query appears to find all records on the last inserted date. Your Sql is approximately equivalent to:
var lastDate = db.Rates.Max(r => r.InsertedDate).Date;
var allRatesOnTheLastDate = db.Rates.Where(r => r.InsertedDate >= lastDate)
.OrderByDescending(r => r.InsertedDate);
This should be more performant than your Sql, given that you are casting the InsertedDate
column which will prevent SARGability of any index on the column.
(Assuming an EF or Linq2Sql context called db
with a Rates entity bound to it)