I think the Entity Framework documentation should recommend one option over the other when the user knows that the query will return one result.
I think the feature of Single() throwing an exception when there is more than one matching element is rarely needed when querying a database because users are normally querying to fetch data, not to validate data already saved in the database.
Single() should have a worse performance in at least some cases since it may require a full table scan to ensure there isn't a second match.
This ASP.NET article recommends First() over Single()
https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/crud?view=aspnetcore-8.0#ways-to-read-one-entity
But I saw @roji recommending Single() over First() in some issue comments
dotnet/efcore#31623 (comment)
dotnet/efcore#29782 (comment)
Also using First() without OrderBy() produces a warning
The query uses the 'First'/'FirstOrDefault' operator without 'OrderBy' and filter operators. This may lead to unpredictable results.
Which makes me think the Entity Framework team wants users to call Single() for queries that return one item.
It will also help if the documentation says when First() performs significantly better that Single(). For example when filtering by a non-indexed column.
I think the Entity Framework documentation should recommend one option over the other when the user knows that the query will return one result.
I think the feature of Single() throwing an exception when there is more than one matching element is rarely needed when querying a database because users are normally querying to fetch data, not to validate data already saved in the database.
Single() should have a worse performance in at least some cases since it may require a full table scan to ensure there isn't a second match.
This ASP.NET article recommends First() over Single()
https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/crud?view=aspnetcore-8.0#ways-to-read-one-entity
But I saw @roji recommending Single() over First() in some issue comments
dotnet/efcore#31623 (comment)
dotnet/efcore#29782 (comment)
Also using First() without OrderBy() produces a warning
Which makes me think the Entity Framework team wants users to call Single() for queries that return one item.
It will also help if the documentation says when First() performs significantly better that Single(). For example when filtering by a non-indexed column.