Skip to content

Union/Concat fails at mapping anonymous types from Select #16725

Closed

Description

@dasepa97

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.

Activity

  1. divega commented on Jul 24, 2019

    @divega
    Contributor

    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.

  2. added this to the 3.0.0 milestone on Jul 26, 2019
  3. added a commit that references this issue on Jul 29, 2019
    1956bd4
  4. roji commented on Jul 29, 2019

    @roji
    Member

    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

  5. added 3 commits that reference this issue on Jul 31, 2019
    71c4e2c
    b6930c4
    e85773b
  6. added a commit that references this issue on Aug 1, 2019
    a3a9bde
  7. modified the milestones: 3.0.0, on Aug 21, 2019
  8. modified the milestones: , 3.0.0 on Nov 11, 2019
  9. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions