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.52k stars 3.13k forks source link

Relational: Consider generating "missing ordering" warning for FirstOrDefault with predicate #33997

Open roji opened 1 month ago

roji commented 1 month ago

In relational, when Skip/Take are used without an OrderBy, we generate a "missing ordering" because results are non-deterministic. We do the same for FirstOrDefault/SingleOrDefault, but for some reason we specifically don't generate the warning if a predicate is provided, e.g. Where(b => b.Posts.FirstOrDefault(b => b.Id > 8)). Whether a predicate is provided or not doesn't have any bearing on the determinism of the resultset (unless it happens to filter by equality to the key etc.).

It's worth noting that FirstOrDefault with predicate can be rewritten to Where+FirstOrDefault without predicate; we generate a warning for the latter but not for the former although they are identical.

roji commented 1 month ago

Design decision: warn also for FirstOrDefault with predicaet.