Skip to content

InvalidOperationException when transalating LINQ query having Count() with predicate for GroupBy. #20546

@rkaushik15

Description

@rkaushik15

I am running into a System.InvalidOperationException when trying to run the following LINQ query:

ss => ss.Set().GroupBy(o => o.CustomerID).Select(g =>
g.Count(o => o.OrderID < 10300))

I am running the application using EFCORE 3.1 and SQL Server.

Steps to reproduce

The following test snippet throws the exception:

var query = (ss => ss.Set<Order>().GroupBy(o => o.CustomerID).Select(g =>
g.Count(o => o.OrderID < 10300)));
var result = query.ToArray();

The test is also a part of the SQLServer Functional Test Suite: 'GroupBy_Property_Select_Count_with_predicate'
and the Exception is thrown when running the test suite against SQL Server. The exception is expected, so is there any particular reason why this functionality is not implemented? And are there any plans to make it available in future releases?

Exception and Stack Trace

Message:
    System.InvalidOperationException : The LINQ expression
'(GroupByShaperExpression:
    KeySelector: (o.CustomerID),
    ElementSelector:(EntityShaperExpression:
        EntityType: Order
        ValueBufferExpression:
            (ProjectionBindingExpression: EmptyProjectionMember)
        IsNullable: False
    )
    )
        .Count(o => o.OrderID < 10300)' could not be translated. Either
rewrite the query in a form that can be translated, or switch to client
evaluation explicitly by inserting a call to either AsEnumerable(),
AsAsyncEnumerable(), ToList(), or ToListAsync(). See
https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Stack Trace:
    
RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression
 methodCallExpression)
    RelationalSqlTranslatingExpressionVisitor.Translate(Expression
expression)
    RelationalProjectionBindingExpressionVisitor.Visit(Expression expression)
    RelationalProjectionBindingExpressionVisitor.Translate(SelectExpression
selectExpression, Expression expression)
    
RelationalQueryableMethodTranslatingExpressionVisitor.TranslateSelect(ShapedQu
eryExpression source, LambdaExpression selector)
    
QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpressi
on methodCallExpression)
    QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
    <>c__DisplayClass9_0`1.<Execute>b__0()
    CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1
compiler)
    QueryCompiler.Execute[TResult](Expression query)
    EntityQueryable`1.GetEnumerator()
    LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
    EnumerableHelpers.ToArray[T](IEnumerable`1 source)
    Enumerable.ToArray[TSource](IEnumerable`1 source)
    QueryAsserter`1.AssertQueryScalar[TResult](Func`2 actualQuery, Func`2
expectedQuery, Boolean assertOrder, Boolean isAsync, String testMethodName)
line 160

Further technical details

EF Core version: 3.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions