dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.82k stars 3.2k forks source link

Query: compilation error for query using EF.Property with GroupBy, project first string property of the group, order by its length #12640

Closed maumar closed 2 years ago

maumar commented 6 years ago

query:

var query = ctx.Employees.Where(
                        e => EF.Property<string>(e, "Title") == "Sales Representative"
                             && e.EmployeeID == 1)
                    .GroupBy(e => EF.Property<string>(e, "Title"))
                    .Select(g => EF.Property<string>(g.First(), "Title"))
                    .OrderBy(e => e.Length);

query model:

from IGrouping<string, Employee> g in 
    (from Employee e in DbSet<Employee>
    where Property([e], "Title") == "Sales Representative" && [e].EmployeeID == 1
    select [e]).GroupBy(Property([e], "Title"), [e])
order by 
    (from Employee <generated>_1 in [g]
    select Property([<generated>_1], "Title")).First().Length asc
select Property(
    (from Employee <generated>_1 in [g]
    select Property([<generated>_1], "Title")).First(), "Title")

interestingly, if using member access directly (rather than EFProperty) everything works fine.

Exception:

Value does not fall within the expected range.
    at System.SharedTypeExtensions.GetSequenceType(Type type)
    at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitMainFromClause(MainFromClause fromClause, QueryModel queryModel)
    at Remotion.Linq.Clauses.MainFromClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel)
    at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
    at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
    at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
    at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitSubQuery(SubQueryExpression expression)
    at Remotion.Linq.Clauses.Expressions.SubQueryExpression.Accept(ExpressionVisitor visitor)
    at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
    at System.Linq.Expressions.ExpressionVisitor.VisitMember(MemberExpression node)
    at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitMember(MemberExpression node)
    at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
    at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
    at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection)
    at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitOrdering(Ordering ordering, QueryModel queryModel, OrderByClause orderByClause, Int32 index)
    at Remotion.Linq.QueryModelVisitorBase.VisitOrderings(ObservableCollection`1 orderings, QueryModel queryModel, OrderByClause orderByClause)
    at Remotion.Linq.QueryModelVisitorBase.VisitOrderByClause(OrderByClause orderByClause, QueryModel queryModel, Int32 index)
    at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitOrderByClause(OrderByClause orderByClause, QueryModel queryModel, Int32 index)
    at Remotion.Linq.Clauses.OrderByClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel, Int32 index)
    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)
    at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
    at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
    at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
    at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_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 Remotion.Linq.QueryableBase`1.GetEnumerator()
    at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
    at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
    at Microsoft.EntityFrameworkCore.Query.GroupByQueryTestBase`1.GroupBy_Shadow(Boolean isAsync)
    at Microsoft.EntityFrameworkCore.Query.GroupByQuerySqlServerTest.<>n__142(Boolean isAsync)
    at Microsoft.EntityFrameworkCore.Query.GroupByQuerySqlServerTest.<GroupBy_Shadow>d__147.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
smitpatel commented 6 years ago

Dependent on #12089

smitpatel commented 5 years ago

Duplicate of #13805

smitpatel commented 5 years ago

Duplicate of #12088