我有 2 个 SQL 语句,它们基本上做同样的事情,即根据一组记录的日期时间字段从表中检索最后一条记录。我正在使用数据优先的实体框架模型。我将如何使用 LINQ Lambda 函数编写这些 SQL 语句?
IE,
var u = db.AccessCodeUsage.Where(...).GroupBy(...)
而不是
var u = from a in db.AccessCodeUsage
where ...
group by ...
SQL 语句:
SELECT *
FROM AccessCodeUsage a
WHERE NOT EXISTS (SELECT 1
FROM AccessCodeUsage
WHERE LocationId = a.LocationId
AND Timestamp > a.Timestamp)
SELECT a.*
FROM AccessCodeUsage a
WHERE a.Timestamp =
(SELECT MAX(Timestamp)
FROM AccessCodeUsage
WHERE a.LocationId = LocationId
AND a.AccessCode = AccessCode
GROUP By LocationId, AccessCode)