Repository navigation
Only translate static Compare methods declared on the operand type - #39491
Merged
AndriySvyryd merged 2 commits intoNov 13, 2026
Merged
Conversation
- ComparisonTranslator and CosmosComparisonTranslator matched any int-returning method named Compare with two arguments of the same type, so an IComparer<T> instance was translated as natural order and its own ordering was ignored - Queries that use a comparer now fail translation in a filter or ordering and are evaluated on the client in a final projection Fixes dotnet#39402
Contributor
Author
|
@dotnet-policy-service agree |
Nullable.Compare<T> is a static Compare method declared on an unrelated type and must remain untranslated by both the relational and Cosmos comparison translators.
AndriySvyryd
approved these changes
Nov 13, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

ComparisonTranslatortranslated anyint-returning method namedComparewith two arguments of the same type into a natural-orderCASE. It never checked which type declared the method, soIComparer<T>.Comparewas translated as if the comparer weren't there:StringComparer.OrdinalIgnoreCase.Compare(x.Name, "abc") == 0becamex.Name = 'abc', and a reversedComparer<int>compared in the normal direction.The
Comparebranch now only matches static methods declared on the operand type (string.Compare,decimal.Compare,DateTime.Compare, ...).CosmosComparisonTranslatorgets the same check.This is a behavior change. Instance comparers, including
Comparer<T>.Defaultas discussed on the issue, and staticComparemethods on unrelated types no longer translate: in a filter or ordering the query now fails with the usual "could not be translated" error, and in a final projection it's evaluated on the client.Nullable.Compare<T>also stops translating, since it's declared onSystem.Nullablerather than onT. Its old translation returned NULL when an operand was null, unlike the method itself, but please say if you'd rather keep it.Tests are in
MiscellaneousTranslationsTestBasewith provider overrides, and no existing baselines changed. I ran Sqlite and InMemory locally. The SQL Server and Cosmos functional tests couldn't run on my machine (only the Cosmos override guard ran), so those baselines haven't been executed. I wrote the SQL Server ones from the Sqlite output and the Cosmos ones fromToQueryStringoutput.Fixes #39402