Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with dynamic 'OrderBy' and AutoMapper inherited types #18584

Closed
onionhammer opened this issue Oct 24, 2019 · 13 comments
Closed

Issue with dynamic 'OrderBy' and AutoMapper inherited types #18584

onionhammer opened this issue Oct 24, 2019 · 13 comments

Comments

@onionhammer
Copy link

Similar description to #11453, but with EF Core 3.1 preview;

If I generate an 'OrderBy' using a dynamic expression, I receive an 'InvalidOperationException',
"...could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either "

If I use the equivalent non-generated code, the query executes fine.

image
image

OrderByExpression() is defined here, and works for simple cases, but when combined with

Steps to reproduce

  1. Ensure docker is installed
  2. Run the following
git clone https://github.com/onionhammer/ReproduceEFCoreIssue.git
cd ReproduceEFCoreIssue
dotnet test

Error Message:
System.InvalidOperationException : The LINQ expression 'OrderByDescending<Classroom, int>(
source: DbSet,
keySelector: (c) => new ClassroomViewModel{
Classroom = c.Name,
NumStudents = Count(Where(
source: DbSet,
predicate: (s) => Property<Nullable>(c, "Id") != null && Property<Nullable>(c, "Id") == Property<Nullable>(s, "ClassroomId"))),
TeacherNames = Select<Teacher, string>(
source: Where(
source: DbSet,
predicate: (t) => Property<Nullable>(c, "Id") != null && Property<Nullable>(c, "Id") == Property<Nullable>(t, "ClassroomId")),
selector: (t) => t.Name)
}
.NumStudents)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
Stack Trace:

Further technical details

EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET Core 3.0)
Operating system:
IDE: (e.g. Visual Studio 2019 16.3)

@smitpatel
Copy link
Contributor

NumStudents likely you have wrong PropertyInfo in your member expression. See #18386

@onionhammer
Copy link
Author

@smitpatel Both expressions are the same, I verified the expression tree

@smitpatel
Copy link
Contributor

In the memberExpression p => p.DateAdded, the memberInfo.ReflectedType is different than what compiler generates. So manually generated expression tree is somewhat incorrect that compiler generated. Hence it does not work. Exact duplicate of #18386

@onionhammer
Copy link
Author

@smitpatel So since this code worked flawlessly for years in EntityFramework, what is it, in practical terms, about EF Core that doesnt like this code

@smitpatel
Copy link
Contributor

// Form1
new ClassroomViewModel {
    Classroom = c.Name,
    NumStudents = s.Students.Count()
}.NumStudents

// Form2
s.Students.Count()

In order for us to convert Form1 to form2, the MemberInfo inside ClassroomViewModel NumStudents must match MemberInfo used in MemberExpression. In your dynamically generated case they are different so we cannot simplify it. So you get error. Given compiler does not generate that kind of expression tree, it is low priority for us to try to support such scenarios.

@onionhammer
Copy link
Author

onionhammer commented Oct 24, 2019

So in other words, it's an open bug, not a closed bug? but you're saying it's low priority?

These expressions look identical to me, left hand side is defined statically, the right hand side is dynamically constructed, left hand side works, right hand side fails

image

@smitpatel
Copy link
Contributor

ReflectedType is different in both sides.
It's closed bug because right now we don't think we will ever get time to fix it.

@onionhammer
Copy link
Author

@smitpatel thanks, I must be blind :)

If anyone is interested, I've updated the GIST with the working 'ObjectQueryExtensions.cs'

@liuliang-wt
Copy link

I also run into this trouble , constructing a dynamic expression is critical to our product. For example, we let user only write basic select query, and we add orderby expression dynamically when he click a list title. If you are not going to fix this, please at least tell me a walk around.

@liuliang-wt
Copy link

@onionhammer hi, have you solved this problem?

@smitpatel
Copy link
Contributor

@liuliang-wt - See #18386 (comment)

@onionhammer
Copy link
Author

@smitpatel
Copy link
Contributor

We are enabling support for this scenario in #19182

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants