-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Using IQueryable with record type - InvalidOperationException #29556
Comments
It seems like you're trying to project to some custom type that's unknown to EF (TestDto), and then compose a filter over that; this isn't supported by EF, since it can't be translated to SQL for evaluation in the database (see Client vs. Server Evaluation). Your first code sample (chained with record) shouldn't work either: var query = _context.myTable.Select(s => new TestDto(s.CellLocationId, s.AreaCode)).Where(x => x.AreaCode == areaCode); If you're seeing something different, please submit a full, runnable code sample so that we can see exactly what you're doing. |
This approach is a Linq feature and is supported as per DTOs (includes a sample project). |
Duplicate of #27281? (EF definitely supports projecting to a class using member initialisation and filtering on that. With a constructor, maybe not.) |
@LeighTheKiwi can you please confirm whether your first code sample (chained with record) works when used with EF? You indicate that it does, although it shouldn't.
That's a very general statement. EF generally supports projecting out to DTOs just fine; what it doesn't support is projecting out to unknown types and then attempting to compose additional LINQ operators over them (e.g. Where). The logic here is that EF must be able to translate your LINQ query to SQL, except for the last projection, which can be evaluated on the client (not translated to SQL) without affecting performance. If you compose a Where after something which EF cannot translate to EF (e.g. instantiation of an unknown type), you're forcing that Where to evaluate at the client, which brings all the data from the database and can be very bad for performance. Again, to understand all this read Client vs. Server Evaluation, One exception to this is anonymous types ( |
As pointed out by @stevendarby, EF does support projecting to a class using member initialisation and filtering on that - but not with a constructor, which is what the OP does. |
@roji - you are correct, I copied the wrong bit of test code - constructor implementation should have been:
Closed as duplicate. |
Duplicate of #27281 |
I am getting an InvalidOperationException when using a projection into a record type (.Net 7). The query works fine if the where clause is chained. It also works if where clause is not chained and the projection is not into a record (class, anonymous type both ok).
This works - chained with record:
This works - unchained with anonymous:
This fails - unchained with record:
EF Core version: 7
LINQ
Database provider:Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows 10 Enterprise
IDE: Visual Studio 2022 17.4.0
The text was updated successfully, but these errors were encountered: