【问题标题】:EF Core 'InMemoryProjectionBindingExpressionVisitor' failed. This may indicate either a bug or a limitation in EF CoreEF Core 'InMemoryProjectionBindingExpressionVisitor' 失败。这可能表示 EF Core 中的错误或限制
【发布时间】: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


【解决方案1】:

感谢@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);

【讨论】:

    猜你喜欢
    • 2020-06-13
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2018-08-28
    • 2018-01-21
    相关资源
    最近更新 更多