【问题标题】:System.InvalidOperationException : Unable to translate the given 'GroupBy' patternSystem.InvalidOperationException:无法翻译给定的“GroupBy”模式
【发布时间】:2022-01-18 12:49:58
【问题描述】:

我有一个来自下面group by 子句的System.InvalidOperationException。我不知道为什么并且已经调试了一段时间但没有运气。非常感谢您。

var performances = await GeneralModelRepository.GetQueryable<Performance>()
                .Where(x => !x.IsDeleted &&
                    x.ActualDateTime.Date >= now.Date && x.ActualDateTime.Date <= now.AddDays(6).Date)
                .GroupBy(x => x.MovieId)
                .AsNoTracking().ToListAsync();

堆栈跟踪:

Message: 
System.InvalidOperationException : Unable to translate the given 'GroupBy' pattern. Call 'AsEnumerable' before 'GroupBy' to evaluate it client-side.

Stack Trace: 
ShaperProcessingExpressionVisitor.VisitExtension(Expression extensionExpression)
Expression.Accept(ExpressionVisitor visitor)
ExpressionVisitor.Visit(Expression node)
ShaperProcessingExpressionVisitor.ProcessShaper(Expression shaperExpression, RelationalCommandCache& relationalCommandCache, LambdaExpression& relatedDataLoaders)
RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQuery(ShapedQueryExpression shapedQueryExpression)
ShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
Expression.Accept(ExpressionVisitor visitor)
ExpressionVisitor.Visit(Expression node)
QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
Database.CompileQuery[TResult](Expression query, Boolean async)

我知道在客户端执行它会更容易,但是否可以在查询本身中执行它?

【问题讨论】:

标签: c# linq entity-framework-core


【解决方案1】:

只需在 AsNoTracking() 之后添加 Select 语句,如下所示。

var performances = await GeneralModelRepository.GetQueryable<Performance>()
                    .Where(x => !x.IsDeleted &&
                        x.ActualDateTime.Date >= now.Date && x.ActualDateTime.Date <= now.AddDays(6).Date)
                    .GroupBy(x => x.MovieId)
                    .AsNoTracking().Select(x=>x.FirstOrDefault()).ToListAsync();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 2022-11-11
    • 2021-05-25
    • 2022-10-20
    • 2010-12-29
    • 2021-05-28
    • 2021-12-28
    相关资源
    最近更新 更多