无法翻译 LINQ 子查询 Where 子句

LINQ Subquery Where Clause could not be translated

我正在将此查询转换为 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。它警告:

The LINQ expression 'where ([r].CreatedAt == [v].CreatedAt)' could not be translated and will be evaluated locally

Core 2.2 是否支持子查询的 where 子句,还是我哪里出错了?

似乎是(许多)EFC 2.2 查询转换器之一 bugs/defects/shortcomings。在 EFC 3.1 中按预期工作。

从所有 "query patterns"(他们这样称呼)中,我能找到的唯一可行的方法是将谓词推入 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