Complete `FirstWithoutOrderByAndFilterWarning` coverage for subqueries

Open
#39,310 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@z0rimo is already working on this.

Since Nov 16, 2026.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
database

Research direction

Start by reviewing PR #39218 and issue #39129 to understand the existing relational warning check; then inspect the separate Cosmos check and query translation paths for TPC, views, and FromSql. The issue lists the remaining cases and required safeguards, but leaves several behavior decisions open. Done means resolving those decisions and covering the supported cases while preserving suppression for explicit ordering, intentional filters, and correlations proven to match at most one row.

Written by the indexing model from the issue text.

Description

area-query

PR #39218 fixed #39129 for unordered First/FirstOrDefault over collection-navigation subqueries whose correlation can be identified from relational foreign-key metadata. Several cases were deliberately left out of scope.

This issue tracks the remaining diagnostic gaps and decisions about extending the warning. Some changes would introduce warnings for queries that previously ran without them; applications configuring the warning to throw would be affected.

Remaining warning coverage
  • TPC collection navigations. Detect unordered First/FirstOrDefault when the dependent source is a TPC hierarchy. Columns projected through the generated UNION may lack the metadata needed by the current FK-correlation check. Account for the concrete tables without assuming that a projected column with missing metadata is a principal-key column.

  • Other sources without usable table-column metadata. Investigate equivalent cases involving views and FromSql. Warn where navigation correlation can be established reliably, without treating arbitrary user filters as relationship correlation.

  • Grouping elements. Decide whether to warn for unordered selections such as GroupBy(...).Select(g => g.First()). These can choose an arbitrary element within each group, but extending the warning affects existing queries and samples.

  • Non-FK correlations. Decide whether and when to warn for queries such as context.Others.Where(o => o.X == b.Y).FirstOrDefault(). A correlation can leave multiple candidate rows even when it does not follow a modeled relationship. Define how to distinguish this from an intentional user filter.

  • Filtered unique indexes. Do not assume every filtered unique index guarantees at most one matching row. For example, a unique index on CustomerId filtered to active rows still permits multiple inactive rows. Preserve suppression for SQL Server's conventional nullable unique indexes: their IS NOT NULL filter excludes rows that an ordinary equality correlation cannot match. The index filter is provider-specific SQL, so this needs a reliable way to establish whether uniqueness applies to the candidate rows.

  • Cosmos parity. Assess the equivalent behavior for supported Cosmos query shapes. Its separate First/FirstOrDefault warning check was unchanged by #39218.

Preserve intentional filters and deterministic selections
  • Do not treat null checks on unrelated outer columns as navigation-generated principal-key guards. For example, c.Orders.FirstOrDefault(o => c.City != null) contains an explicit user filter.
  • Account for user filters stored in SelectExpression.Having, not only Predicate. For example, c.Orders.GroupBy(o => o.CustomerID).Where(g => g.Count() > 1).Select(g => g.Count()).FirstOrDefault() has an explicit filter and at most one matching group.
  • Continue suppressing the warning for explicit ordering and correlations proven to match at most one row. Do not infer relationship-key provenance merely from missing column metadata.
Dominant language
C#
Stars
14.8k
Forks
3.5k
Avg merge
3d 7h
Merged PRs (30d)
182

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.