Jerome
Jerome

Reputation: 42

System.InvalidOperationException : Unable to translate the given 'GroupBy' pattern

I have a System.InvalidOperationException from the group by clause below. I am not sure why and have been debugging for a while now but no luck. Thank you so much in advance.

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();

Stack trace:

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)

I know it would be easier to do it client-side, but is it possible to do it in the query itself?

Upvotes: 0

Views: 152

Answers (1)

Kiran Joshi
Kiran Joshi

Reputation: 1876

Just add Select statement after AsNoTracking() like below.

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();

Upvotes: 1

Related Questions