【发布时间】:2023-04-02 02:45:01
【问题描述】:
我在 EF Core 2.2 中将此查询转换为 linq,但我找不到任何正确的方法:
select r.*,v.* from Planning.RefineryUnitMonthDatas r
join Planning.RefineryUnitMonthDataValues v on r.Id = v.EntityId
join(
select EntityId, max(v.CreatedAt) CreatedAt from Planning.RefineryUnitMonthDatas r
join Planning.RefineryUnitMonthDataValues v on r.Id = v.EntityId
where RefineryUnitId = @refinery and Date / 100 = @date
group by EntityId
) t on r.Id = t.EntityId
where t.CreatedAt = v.CreatedAt
这是我的 linq:
from s in db.RefineryUnitMonthDatas
join v in db.RefineryUnitMonthDataValues on s.Id equals v.EntityId
join r in (
from r in db.RefineryUnitMonthDatas
join v in db.RefineryUnitMonthDataValues on r.Id equals v.EntityId
where r.RefineryUnitId == refinery && r.Date / 100 == date
group v by v.EntityId into g
select new { g.Key, CreatedAt = g.Max(s => s.CreatedAt) }
) on s.Id equals r.Key
where r.CreatedAt == v.CreatedAt
select new { s, v }
这可以正常工作,但存在性能问题,并且最后一个 Where 子句未转换为 sql。它警告:
LINQ 表达式“where ([r].CreatedAt == [v].CreatedAt)”不能 翻译并在本地评估
Core 2.2 是否支持子查询的 where 子句或我在某处犯了错误?
【问题讨论】:
标签: c# sql linq ef-core-2.2