Repository navigation
Description
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
Add test to verify dotnet#7417 is fixed
Add support for translating GROUP BY and OUTER APPLY; optimize transl…
Add support for translating GROUP BY and OUTER APPLY; optimize transl…
Add support for translating GROUP BY and OUTER APPLY; optimize transl…
Add support for translating GROUP BY and OUTER APPLY; optimize transl…
Add support for translating GROUP BY and OUTER APPLY; optimize transl…
Add support for translating GROUP BY and OUTER APPLY; optimize transl…
maumar commentedon Apr 17, 2017
This works in current bits. We produce the following sql:
[-]GroupJoin and SelectMany fails to translate[/-][+]Query: GroupJoin and SelectMany fails to translate[/+]