Skip to content

Query: GroupJoin and SelectMany fails to translate #7417

Closed

Description

@Dixin

I am porting EF code to EF Core, and got this error. GroupJoin returns hierarchical data, so use SelectMany to flatten it:

var leftOuterJoin = adventureWorks.Products
    .GroupJoin(
        inner: adventureWorks.Transactions,
        outerKeySelector: product => product.ProductID,
        innerKeySelector: transaction => transaction.ProductID,
        resultSelector: (product, transactions) => new
        {
            Product = product,
            Transactions = transactions
        })
    .SelectMany(
        productTransactions => productTransactions.Transactions,
        (productTransactions, transaction) => new
        {
            Name = productTransactions.Product.Name,
            ActualCost = transaction.ActualCost
        });

In EF, the translation is correct:

SELECT 
    [Extent1].[ProductCategoryID] AS [ProductCategoryID], 
    [Extent1].[Name] AS [Name], 
    [Extent2].[Name] AS [Name1]
    FROM  [Production].[ProductCategory] AS [Extent1]
    LEFT OUTER JOIN [Production].[ProductSubcategory] AS [Extent2] ON [Extent1].[ProductCategoryID] = [Extent2].[ProductCategoryID]

In EF Core, the translation fails, and throws exception:

{System.InvalidOperationException: No coercion operator is defined between types 'ConsoleApp1.ProductSubcategory' and 'System.String'.
at System.Linq.Expressions.Expression.GetUserDefinedCoercionOrThrow(ExpressionType coercionType, Expression expression, Type convertToType)
at Remotion.Linq.Parsing.RelinqExpressionVisitor.d__0.MoveNext()
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source, Int32& length)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
at System.Dynamic.Utils.CollectionExtensions.ToReadOnly[T](IEnumerable`1 enumerable)
at System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable`1 arguments, IEnumerable`1 members)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.<>c__DisplayClass67_0.b__0(Expression e) in D:\Dixin\GitHub\EntityFramework\src\Microsoft.EntityFrameworkCore.Relational\Query\RelationalQueryModelVisitor.cs:line 749
at Remotion.Linq.Clauses.SelectClause.TransformExpressions(Func`2 transformation)
at Remotion.Linq.QueryModel.TransformExpressions(Func`2 transformation)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.OptimizeJoinClause(JoinClause joinClause, QueryModel queryModel, Int32 index, Action baseVisitAction, MethodInfo operatorToFlatten, Boolean groupJoin) in D:\Dixin\GitHub\EntityFramework\src\Microsoft.EntityFrameworkCore.Relational\Query\RelationalQueryModelVisitor.cs:line 748
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitGroupJoinClause(GroupJoinClause groupJoinClause, QueryModel queryModel, Int32 index) in D:\Dixin\GitHub\EntityFramework\src\Microsoft.EntityFrameworkCore.Relational\Query\RelationalQueryModelVisitor.cs:line 623
at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection`1 bodyClauses, QueryModel queryModel)
at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel) in D:\Dixin\GitHub\EntityFramework\src\Microsoft.EntityFrameworkCore\Query\EntityQueryModelVisitor.cs:line 721
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel) in D:\Dixin\GitHub\EntityFramework\src\Microsoft.EntityFrameworkCore.Relational\Query\RelationalQueryModelVisitor.cs:line 373
at Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryModelVisitor.VisitQueryModel(QueryModel queryModel) in D:\Dixin\GitHub\EntityFramework\src\Microsoft.EntityFrameworkCore.SqlServer\Query\Internal\SqlServerQueryModelVisitor.cs:line 92
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) in D:\Dixin\GitHub\EntityFramework\src\Microsoft.EntityFrameworkCore\Query\EntityQueryModelVisitor.cs:line 259
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel) in D:\Dixin\GitHub\EntityFramework\src\Microsoft.EntityFrameworkCore\Storage\Database.cs:line 67}

The exception is from RelationalQueryModelVisitor.OptimizeJoinClause:

queryModel.TransformExpressions(e =>
    ReferenceReplacingExpressionVisitor
        .ReplaceClauseReferences(
            e,
            querySourceMapping,
            throwOnUnmappedReferences: false));

Activity

  1. added this to the 2.0.0 milestone on Jan 17, 2017
  2. maumar commented on Apr 17, 2017

    @maumar
    Contributor

    This works in current bits. We produce the following sql:

    SELECT [product].[Name], [transaction].[ActualCost]
    FROM [Products] AS [product]
    INNER JOIN [Transactions] AS [transaction] ON [product].[ProductID] = [transaction].[ProductID]
  3. changed the title [-]GroupJoin and SelectMany fails to translate[/-] [+]Query: GroupJoin and SelectMany fails to translate[/+] on May 9, 2017
  4. modified the milestones: , 2.0.0 on Oct 15, 2022
  5. 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

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions