-
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
.First()
on SqlQuery results in exception
#31623
Comments
Duplicate of #30447 |
@vanillajonathan tl;dr just name the column coming out of your SQL query |
@roji The docs you linked to does not mention "AS Value". |
@vanillajonathan what am I missing? |
@roji My apologies, I missed it. |
For me it was also not so clear, because the document used the value for the where statement and @vanillajonathan didn't used that (and me also not). And I want an other result (field) than the where clause is from. So I made this based on the documentation: Update: |
@vierlijner I'm not quite sure exactly what it is you tried and why, or what exact examples/docs would have helped here.
What value would there be in putting something in the docs about this, given that EF itself already says everything there is to know in the warning itself? |
Put in the docs how to get a single value with .First() or .FistOrDefault() as an example with the OrderBy for example for the SqlQuery() instruction. |
If you're expecting a query to only return a single result, it's recommended to use .Single() or .SingleOrDefault() - no warnings are issued for those (and an exception is generated if there's more than one result). In general, I believe that where we report an informative, useful warning (such as the above), there's little value in also doing exhaustive documentation; users ideally get the warning "just in time" - when they're trying to problematic thing - and it's unlikely they'd read all of our query docs up-front. In addition, adding more and more docs bloats them, and makes it hard for users actually find what they're looking for; if we documented every little corner of the query pipeline, you'd end up with a massive, unreadable document. So we prefer to inform users of problems via warnings/errors, and to strike a good balance when explicitly documenting things. |
Interestingly enough using
SqlQuery
withFirst()
does not work. However doingToList().First()
works.Does not work:
However using
ToList().First()
does work:Stack trace
Include provider and version information
EF Core version: 7.0.10
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 6.0
Operating system: Windows 11 (22621.2134)
IDE: Visual Studio 2022 17.7.3
The text was updated successfully, but these errors were encountered: