Efcore: Union/Concat fails at mapping anonymous types from Select

Created on 24 Jul 2019  路  2Comments  路  Source: dotnet/efcore

I have the following code:
image
Which produces following exception:

Exception message: Non-matching or unknown projection mapping type in set operation
Stack trace:
   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.SqlExpressions.SelectExpression.ApplySetOperation(SetOperationType setOperationType, SelectExpression otherSelectExpression, Expression shaperExpression)
   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateUnion(ShapedQueryExpression source1, ShapedQueryExpression source2)
   at Microsoft.EntityFrameworkCore.Query.Pipeline.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at Microsoft.EntityFrameworkCore.Query.Pipeline.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
   at Microsoft.EntityFrameworkCore.Query.Pipeline.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__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()

Now for some details:
This was done in unit test using Xunit and Sqlite memory database.
All three Selects provide anonymous type consisting of (int, DateTime, DateTime, int) which somehow cannot be treated neither with Union nor Concat (same mapping exception as above).
Using .ToList() that are commented in above example bypasses the problem and provides correct answer, but as expected splits whole operation into three separate database queries that are later glued together on client and that I would like to avoid. GroupBy translates into SQL as expected so big thanks to the team for putting it in preview 7.
Casting proved to be irrelevant, I have tried using only one union with no (DateTime?) and it ends up with the same problem. I checked couple of other ideas like not using names for selected fields but to no avail. It also does not matter whether selects provided any data or not.

closed-fixed customer-reported type-bug

All 2 comments

Note for triage: I talked to @smitpatel and also verified with some testing. There are a couple of things in this query that can throw off the logic that tries to match the projections and trigger the exception:

  1. The Source constant
  2. The GroupBy and aggregate functions

We need to evaluate the priority of fixing all or some of these issues. We can also evaluate possible workarounds like using FromSql methods.

The issue here is that the current set operation implementation is needlessly restrictive on the type of expressions it allows on the sub-queries, and above you have a function call (count) which causes the error.

Submitted fix in #16813

Was this page helpful?
0 / 5 - 0 ratings