我找不到为什么此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,则联接的行数可能会太高,有时我会得到OutOfMemmory错误或超时错误。
有人知道如何将此查询转换为Entity Framework Core 3.1.5上的有效查询吗? 预先感谢!
答案 0 :(得分:2)
感谢@IvanStoev的提示,该错误已通过在GroupBy上显式投影所需的字段而消失:
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})
.GroupBy(@t => new {@t.c.Year, @t.c.Month}, @t => @t.c.Attribute)
.Select(cg => new
{
cg.Key.Year,
cg.Key.Month,
P1 = cg.Average(i => i.P1),
P2 = cg.Average(i => i.P2),
P3 = cg.Average(i => i.P3),
P4 = cg.Average(i => i.P4),
P5 = cg.Average(i => i.P5),
P6 = cg.Average(i => i.P6),
Total = cg.Average(i => i.Total),
N = cg.Count(),
NumberOfPeriods = 1
})
.OrderBy(@t => @t.Year)
.ThenBy(@t => @t.Month);