【发布时间】:2020-11-09 19:06:21
【问题描述】:
我找不到这个 IQueryable 不起作用的原因,我尝试了一些不同的方法,但总是退回到相同的错误,'InMemoryProjectionBindingExpressionVisitor' 失败。这可能表明 EF Core 中存在错误或限制:
var q = _QueryService.GetSomeInformation(db, cp.Attributes, min, max);
var query = q
.Join(db.SomeTable
.Where(mc => mc.Attribute.Total >= minimumValue),
s => s.Code,
c => c.Code,
(s, c) => new {s, c})
//.AsEnumerable()
.GroupBy(@t => new {@t.c.Year, @t.c.Month}, @t => @t.c)
.Select(cg => new
{
cg.Key.Year,
cg.Key.Month,
P1 = cg.Average(i => i.Attribute.P1),
P2 = cg.Average(i => i.Attribute.P2),
P3 = cg.Average(i => i.Attribute.P3),
P4 = cg.Average(i => i.Attribute.P4),
P5 = cg.Average(i => i.Attribute.P5),
P6 = cg.Average(i => i.Attribute.P6),
Total = cg.Average(i => i.Attribute.Total),
N = cg.Count(),
NumberOfPeriods = 1
})
.OrderBy(@t => @t.Year)
.ThenBy(@t => @t.Month);
如果我取消注释“.AsEnumerable()”,它可以完美地在客户端执行,但是如果没有在服务器端执行 GroupBy,连接的行数可能会太高,有时我会得到 OutOfMemory错误或超时错误。
有人知道如何将此查询转换为 Entity Framework Core 3.1.5 上的有效查询吗? 提前致谢!!
【问题讨论】:
-
我猜这是 EF Core 3.1 的问题。看起来没有任何ETA。 github.com/dotnet/efcore/issues/17620
-
EF Core 版本?
-
@IvanStoev EF Core 3.1.5
-
无法给出不带repro的具体答案,但据我所知,这可能与
GroupBy之后导航属性的使用有关。尝试通过显式投影您需要的所有内容来消除它,例如.GroupBy(..., @t => new { @t.c.Attribute }). -
感谢@IvanStoev,它解决了错误,但我仍然有一些超时错误,看来翻译后的查询不如以前优化了。
标签: c# entity-framework-core iqueryable