dotnet / dotnet/efcore

Set operation over subquery projecting a collection throws because of client evaluation

Open
#32,977 10 comments 0 reactions 0 assignees View on GitHub
area-query area-set-operations customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

I'm seeing the following error when trying to Concat two conditionally projected Queryables:

`Error creating query string: Unable to translate set operation after client projection has been applied. Consider moving the set operation before the last 'Select'`

~~My code is roughly equivalent to the following:~~
*(See https://github.com/dotnet/efcore/issues/32977#issuecomment-1919636527 for an updated minimal repro)*
```C#
var queryA = _dbContext.EntityA.Select(x => new MergedEntity {
aTitle = x.Title,
bTitle = null
});

var queryB = _dbContext.EntityB.Select(x => new MergedEntity {
aTitle = null,
bTitle = x.Title
});

queryA.Concat(queryB).ToList();
```

I found a previous issue talking about this behaviour, but I can't tell how to apply the workaround to my use case:
https://github.com/dotnet/efcore/issues/21148#issuecomment-639481421

and
https://github.com/dotnet/efcore/issues/16243#issuecomment-1136290604

Any help would be greatly appreciated.

### Stack trace
```
System.InvalidOperationException: Unable to translate set operation after client projection has been applied. Consider moving the set operation before the last 'Select' call.
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplySetOperation(SetOperationType setOperationType, SelectExpression select2, Boolean distinct)
at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplyUnion(SelectExpression source2, Boolean distinct)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateConcat(ShapedQueryExpression source1, ShapedQueryExpression source2)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at DevExtreme.AspNet.Data.Async.ReflectionAsyncAdapter.InvokeToListAsync[T](MethodInfo method, IQueryProvider provider, Expression expr, CancellationToken cancellationToken)
at DevExtreme.AspNet.Data.DataSourceLoaderImpl`1.ExecExprAsync[R](Expression expr)
at DevExtreme.AspNet.Data.DataSourceLoaderImpl`1.LoadAsync()
at XXX.XXX.Module.Workflow.Controllers.MixedUserTaskController.GetOpenTasksAndListsDxQuery(Nullable`1 my, Nullable`1 id, DataSourceLoadOptions loadOptions)
```

### Provider and version information

EF Core version: 7.0.11
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: .NET 7.0
Operating system: Windows 11 21H2
IDE: Jetbrains Rider 2023.3.3

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.