Unable to cast object of type 'System.Linq.Expressions.Expression`1.....] to type 'System.Linq.Expressions.ConstantExpression'.
When I run this query:
var list = context.Set<Employee>()
.Select(x => new {
Department_BusinessUnit_Code = x.Department.BusinessUnit.Code,
Manager_Department_BusinessUnit_Code = x.Manager.Department.BusinessUnit.Code,
});
I get this error:
Unable to cast object of type 'System.Linq.Expressions.Expression`1[System.Func`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor+TransparentIdentifier`2[ERP.Model.Employee,System.Collections.Generic.IEnumerable`1[ERP.Model.Employee]],ERP.Model.Employee],System.Collections.Generic.IEnumerable`1[ERP.Model.Department]],ERP.Model.Department],System.Collections.Generic.IEnumerable`1[ERP.Model.BusinessUnit]],ERP.Model.BusinessUnit],System.Int64]]' to type 'System.Linq.Expressions.ConstantExpression'.
Note, this has only started happening with the latest updates to EntityFramework (version 1.0.0-rc2-20100)
Also, if i change the order of the select fields around it works correctly.
The context/models are as follows:
//Models
public class Employee
{
public long ID { get; set; }
public long? ManagerID { get; set; }
public virtual Employee Manager { get; set; }
public long DepartmentID { get; set; }
public virtual Department Department { get; set; }
}
public class Department
{
public long ID { get; set; }
public long? BusinessUnitID { get; set; }
public virtual BusinessUnit BusinessUnit { get; set; }
}
public class BusinessUnit
{
public long ID { get; set; }
public string Code { get; set; }
}
This is a huge show stopper for us. Please, any help here.
I have same problem (on latest sources from dev branch).
` Callstack:
Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.QueryFlattener.Flatten(System.Linq.Expressions.MethodCallExpression methodCallExpression = {System.Linq.Expressions.MethodCallExpression5}) Line 47 C# Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.OptimizeJoinClause(Remotion.Linq.Clauses.JoinClause joinClause = {Remotion.Linq.Clauses.JoinClause}, Remotion.Linq.QueryModel queryModel = {Remotion.Linq.QueryModel}, int index = 3, System.Action baseVisitAction = {Method = {System.Reflection.RuntimeMethodInfo}}, System.Reflection.MethodInfo operatorToFlatten = {System.Reflection.RuntimeMethodInfo}, bool outerJoin = false) Line 484 C# Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitJoinClause(Remotion.Linq.Clauses.JoinClause joinClause = {Remotion.Linq.Clauses.JoinClause}, Remotion.Linq.QueryModel queryModel = {Remotion.Linq.QueryModel}, int index = 3) Line 373 C# Remotion.Linq.dll!Remotion.Linq.Clauses.JoinClause.Accept(Remotion.Linq.IQueryModelVisitor visitor, Remotion.Linq.QueryModel queryModel, int index) Unknown Remotion.Linq.dll!Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(System.Collections.ObjectModel.ObservableCollection<Remotion.Linq.Clauses.IBodyClause> bodyClauses, Remotion.Linq.QueryModel queryModel = {Remotion.Linq.QueryModel}) Unknown Remotion.Linq.dll!Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(Remotion.Linq.QueryModel queryModel = {Remotion.Linq.QueryModel}) Unknown Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(Remotion.Linq.QueryModel queryModel = {Remotion.Linq.QueryModel}) Line 539 C# Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(Remotion.Linq.QueryModel queryModel = {Remotion.Linq.QueryModel}) Line 256 C# Microsoft.EntityFrameworkCore.SqlServer.dll!Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryModelVisitor.VisitQueryModel(Remotion.Linq.QueryModel queryModel = {Remotion.Linq.QueryModel}) Line 79 C# Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor<LonDatSources.Wpf.Components.CoreGrids.CoreGridDataRowValue>(Remotion.Linq.QueryModel queryModel = {Remotion.Linq.QueryModel}) Line 164 C# Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery<LonDatSources.Wpf.Components.CoreGrids.CoreGridDataRowValue>(Remotion.Linq.QueryModel queryModel = {Remotion.Linq.QueryModel}) Line 67 C# [Native to Managed Transition]
[Managed to Native Transition]
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQuery.AnonymousMethod__0() Line 175 C# Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery<System.Collections.Generic.IEnumerable<LonDatSources.Wpf.Components.CoreGrids.CoreGridDataRowValue>>(object cacheKey = {Microsoft.EntityFrameworkCore.Query.Internal.SqlServerCompiledQueryCacheKeyGenerator.SqlServerCompiledQueryCacheKey}, System.Func<System.Func<Microsoft.EntityFrameworkCore.Query.QueryContext, System.Collections.Generic.IEnumerable<LonDatSources.Wpf.Components.CoreGrids.CoreGridDataRowValue>>> compiler = {Method = {System.Reflection.RuntimeMethodInfo}}) Line 33 C# Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQuery<System.Collections.Generic.IEnumerable<LonDatSources.Wpf.Components.CoreGrids.CoreGridDataRowValue>>(System.Linq.Expressions.Expression query = {System.Linq.Expressions.MethodCallExpressionN}) Line 136 C# Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute<System.Collections.Generic.IEnumerable<LonDatSources.Wpf.Components.CoreGrids.CoreGridDataRowValue>>(System.Linq.Expressions.Expression query = {System.Linq.Expressions.MethodCallExpressionN}) Line 82 C# Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute<System.Collections.Generic.IEnumerable<LonDatSources.Wpf.Components.CoreGrids.CoreGridDataRowValue>>(System.Linq.Expressions.Expression expression = {System.Linq.Expressions.MethodCallExpressionN}) Line 37 C# LonDatSources.Wpf.Components.dll!LonDatSources.Wpf.Components.CoreGrids.CoreGridDataProvider.ExecuteSelect(int from = 0, int size = 400) Line 540 C# `
duplicate of #4539
@GunterGlad Currently a workaround is breaking this into two parts:
var query1 = context.Set<Employee>()
.Select(x => x.Department.BusinessUnit.Code);
var query2 = context.Set<Employee>()
.Select(x => x.Manager.Department.BusinessUnit.Code);
Thanks for the suggested workaround. But we have a dynamic generated select (Use picks what they want to include). That then generates the Linq expression. So unfortunately we cant use that solution. But thanks for the suggestion.
This caused me a couple hours of consternation. Glad it has been fixed.
My workaround had to be executing my query and then performing further selections on it.