Skip to content
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

Improve Query Generation for .CompareTo() #21378

Closed
dbrownems opened this issue Jun 22, 2020 · 2 comments
Closed

Improve Query Generation for .CompareTo() #21378

dbrownems opened this issue Jun 22, 2020 · 2 comments

Comments

@dbrownems
Copy link

dbrownems commented Jun 22, 2020

.NET does not allow comparison operators >,<,<=,>= on strings. SQL Server does. The only way to express range predicates is with string.CompareTo(string). But EF translates them like this

var col = db.Customers.Where(c => c.Name.CompareTo("J") < 0).ToList();

becomes

      SELECT [c].[Id], [c].[Name]
      FROM [Customers] AS [c]
      WHERE CASE
          WHEN [c].[Name] = N'J' THEN 0
          WHEN [c].[Name] > N'J' THEN 1
          WHEN [c].[Name] < N'J' THEN -1
      END < 0

This query must perform a scan of all the customers' Names to apply the predicate. A better translation would be

      SELECT [c].[Id], [c].[Name]
      FROM [Customers] AS [c]
      WHERE [c].[Name] < N'J'

Which can perform a seek to the starting location if an index is present.

Alternatively methods could be added to EF.Functions for Between, LessThan, LessThanOrEqualTo, GreaterThan, and GreaterThanOrEqualTo.

@smitpatel
Copy link
Member

@maumar

@ajcvickers ajcvickers added this to the 5.0.0 milestone Jun 26, 2020
@maumar
Copy link
Contributor

maumar commented Jul 1, 2020

dupe of #16092 which has been fixed in 5.0 preview 6. @dbrownems in order to take advantage of the improvement you need to do equality comparison to 1, 0, -1, rather than > 0, < 0 etc

@maumar maumar closed this as completed Jul 1, 2020
@maumar maumar removed this from the 5.0.0 milestone Jul 1, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants