c# - 无法翻译 LINQ 子查询 Where 子句
问题描述
我在 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 子句或我在某处犯了错误?
解决方案
似乎是(许多)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的比较。
推荐阅读
- python - Pygame Platformer - 随机生成敌人
- php - 如何动态操作传递给 Model::find 的数据?
- wordpress - 如何通过 docker compose cammand 选项添加插件?
- angular - Angular 6 - 从子模块到父模块的事件发射器
- html - 如何通过键盘交互浏览 SVG 图像
- java - 不同的项目 SDK 和语言级别
- laravel - 如果 jwt 令牌在 laravel 和 Vue spa 中使用 httponly 存储在 cookie 中,如何防止 csrf 攻击?
- javascript - Javascript fetch api 返回 200 和响应但是.then 不能工作
- java - 如何在我的自定义类中处理 onClick 或 onTouch 事件?
- javascript - TypeError:无法读取 Reactjs 中未定义的属性“0”添加到购物车功能