我正在将该查询转换为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
这是我的名字:
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子句,或者我在某个地方犯了错误?
答案 0 :(得分:1)
似乎是(许多)EFC 2.2查询翻译器的错误/缺陷/缺点之一。可以在EFC 3.1中正常工作。
在所有“查询模式”(如它们所说的)中,我唯一能找到的一项工作就是将谓词推入join
子句中,例如替换
) on s.Id equals r.Key
where r.CreatedAt == v.CreatedAt
类似
) on new { Key = s.Id, v.CreatedAt } equals new { r.Key, r.CreatedAt }
当然,解决方法仅适用于==
比较,可以将其转换为 equi-join 。