0

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?

Jerome
  • 42
  • 1
  • 6
  • Does this answer your question? [LINQ group by in Entity Framework Core 3.1](https://stackoverflow.com/questions/60002713/linq-group-by-in-entity-framework-core-3-1) – gilliduck Jan 18 '22 at 03:30
  • Actually this GropBy on server side has no sense, because you retrieve all records anyway. – Svyatoslav Danyliv Jan 18 '22 at 06:05

1 Answers1

1

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();
Kiran Joshi
  • 1,758
  • 2
  • 11
  • 34