Skip to content

Query: Consider translating String.Equals(String, StringComparison) for selected values of StringComparison #1222

Description

@divega

Currently we support the == operator and the String.Equals(String) methods which both have the same behavior in-memory as String.Equals(String, StringComparison.Ordinal). We could also support the latter without making significant changes and possibility other values of StringComparsion such as OrdinalIgnoreCase by applying LOWER() on both operands.

Activity

  1. changed the title [-]Query: Consider options for translating String.Equals(String, StringComparison) for selected values of StringComparison[/-] [+]Query: Consider translating String.Equals(String, StringComparison) for selected values of StringComparison[/+] on Dec 4, 2014
  2. added this to the Backlog milestone on Dec 8, 2014
  3. TrabacchinLuigi commented on Feb 1, 2016

    @TrabacchinLuigi

    Actually I just got a warning about String.Equals (without the third parameter) not being able to be translated and being evaluated locally ...

  4. JaymzZh commented on Dec 26, 2016

    @JaymzZh

    After upgrade to 1.1.0, I use String.Equals(String, StringComparison.Ordinal) got this error :

    InvalidOperationException: variable '__name_0' of type 'System.String' referenced from scope '', but it is not defined
    
  5. divega commented on Dec 26, 2016

    @divega
    ContributorAuthor

    @jeffiy can you please create a new issue and provide a repro? If this is a regression in 1.1 we may want to fix it in 1.1.1.

  6. akamud commented on Dec 28, 2016

    @akamud

    Could this be implemented using Collation on SQL Server? Using LOWER on indexed columns throws the index away for querying, as far as I know, which could hurt performance for people using this.

    We wouldn't know this is happening since this would be translated by EF internally and would get us by surprise. Since I would have no warning about "local evaluation" on this case, I would expect that my ORM is doing the right thing and not creating a performance problem. Doing a LOWER should be an explicit decision, in my opinion.

  7. Suriman commented on Jun 22, 2017

    @Suriman

    @divega Please, it is a must have.

  8. xperiandri commented on Jan 25, 2018

    @xperiandri

    Still does not work in 2.0
    I was shocked that it doesn't!

  9. xperiandri commented on Jan 25, 2018

    @xperiandri

    I used string.Equals(string, string)

  10. mgolois commented on May 3, 2018

    @mgolois

    Any update on this?

  11. mgolois commented on Jul 28, 2018

    @mgolois

    Migrating to EFCore is becoming a show stopper for us because this issue. Any update @divega @rowanmiller ?

  12. divega commented on Aug 2, 2018

    @divega
    ContributorAuthor

    @mgolois This issue is about String.Equals(String, StringComparison). could you please elaborate about what exactly the query looks like that you are trying to port to EF Core?

  13. divega commented on Aug 2, 2018

    @divega
    ContributorAuthor

    @mgolois to be more precise:

    1. EF6 completely ignores the StringComparison argument. Is this the behavior that you expect from EF Core? If yes, why?
    2. Also, are you aware that ==, string.Equals(String), and String.Equals(string, string) work? If yes, why is lack of support for String.Equals(String, StringComparison) blocking you?
  14. removed this from the Backlog milestone on Aug 2, 2018
  15. 105 remaining items

  16. kcaswick commented on Jul 10, 2023

    @kcaswick

    I don't want this illusion. What I want is that when I write the C# expression that exactly matches the database behavior, it is translated, rather than triggering an error.

    In various cases, that's simply not possible. For example, you may be running your EF application against some database which EF did not create; we do not know the collation of the column you're querying, and so cannot know whether the comparison will be case-sensitive or not. EF cannot inspect the schema of each and every column in your database just in order to know what the collation is and how comparisons against that column will operate.

    I was under the impression EF already could know the collation of each and every column, because you can define it as shown in collations and case sensitivity. If that is not the case, I still think the platform should provide the functionality for identifying the "collation" used by a comparison expression, rather than having to rewrite that in each provider, some providers only support StringComparison, others only support CompareInfo from the locale, etc. Then I would send the request to allow matching collations to the team for the provider for MS SQL Server (and any other databases I'm using).

    If it is possible to specify collation in the model, but the developer has not, just throw an error that gives enough info for the developer to realize they need to specify it.

    Otherwise, even when what you are doing is possible with all the providers, it is impossible† to share a filter expression between logic running against databases, web APIs, and lists.

    This is the (possibly unfortunate) reality: LINQ is a nice abstraction, but it is necessarily leaky because the query is executed differently based on where it's executed. There are many behavioral discrepancies between where queries execute, around case-sensitivity, null semantics, which functions can be translated, and many other factors. By design, EF does not attempt to gloss over and make queries execute the same wherever executed; that is mostly impossible, and where possible, can lead to significant perf degradation.

    Yes, but it is also preventing the developer from doing it when it is possible, and at least in the case of specified collation, if only the one matching the model is allowed, it should not cause perf degradation. It's preventing the simplest, most common case too, when the only difference is default case-sensitivity, and even the simplest C# expression for case-insensitive compare is not allowed.

    I agree that unspecified/default collation (==) should continue to be allowed, and leak the underlying behavior. Only when the expression specifies the collation to compare with (ex. String.Equals(String, StringComparison.Ordinal)) should it be disallowed against non-matching database collations.

  17. roji commented on Jul 10, 2023

    @roji
    Member

    I was under the impression EF already could know the collation of each and every column, because you can define it as shown in collations and case sensitivity.

    As I wrote above, that's only if you're using EF to create the database. But you could be using EF to connect to an existing database that's managed externally. It's not practical for various reasons to ask EF to discover schema details about all columns you'll potentially be querying.

    An additional friction point here is that database collations can be very elaborate/complex, and don't necessarily correspond to .NET's StringComparison enum; to get an idea, see the SQL Server collations page (and remember that's just one database in what we have to support). Since StringComparison cannot capture the richness of the database collation, we really cannot say that the comparison will occur in the query as it would have occurred in .NET; this is frequently something that English-only devs miss (case sentivity just isn't enough: what about accent sensitivity, kana sensitivity...).

    And at the end of the day, say we implemented this; this would mean that you can start using e.g. StringComparison.OrdinalIgnoreCase, and it would work assuming you've configured your column with the appropriate collation, and throw otherwise. Just like today, the dev would have to go and make sure the column collation is insensitive - that must happen either way. So the only value here is somewhat better discoverability of the fact that you've misconfigured your column collation when you get it wrong; it's not nothing, but I don't think it makes sense in the grand scheme of things.

    EF translates from one domain (.NET/LINQ) to another (relational database/SQL); there are mismatches along the way, both between the source and target domains and between the different target domains. This is unfortunately something that devs must accept and embrace.

  18. dahlbyk commented on Jul 10, 2023

    @dahlbyk

    Put another way, if a particular column is meant to match case-sensitively or not then very often the optimal way to specify that is in the database rather than scattered in the EF code. Unless you really like table scans.

    Thus my request to turn off the compiler warnings suggesting the CultureInfo/StringComparison overloads that we actually don't want to use because the database should be configured for the right comparison.

  19. kcaswick commented on Jul 11, 2023

    @kcaswick

    I was under the impression EF already could know the collation of each and every column, because you can define it as shown in collations and case sensitivity.

    As I wrote above, that's only if you're using EF to create the database. But you could be using EF to connect to an existing database that's managed externally. It's not practical for various reasons to ask EF to discover schema details about all columns you'll potentially be querying.

    Does adding UseCollation to the model force you to use a migration, or will it currently be ignored if you don't run a migration? Since we're using DACPAC, not migrations, and we call UseCollation, I'm pretty sure it doesn't cause problems.

    On projects I've been working on, we've usually used the command line tool to scaffold the context, since we were not usually using migrations. That already exports information about collations, at least for some providers.

    An additional friction point here is that database collations can be very elaborate/complex, and don't necessarily correspond to .NET's StringComparison enum; to get an idea, see the SQL Server collations page (and remember that's just one database in what we have to support). Since StringComparison cannot capture the richness of the database collation, we really cannot say that the comparison will occur in the query as it would have occurred in .NET; this is frequently something that English-only devs miss (case sentivity just isn't enough: what about accent sensitivity, kana sensitivity...).

    Yes, I expected that for some collations there would be no match for the database collation in .Net. I don't have statistics on global collation use, but I bet a very large fraction use one of Latin1_General_100_CI_AS_SC, Latin1_General_100_CI_AS_SC_UTF8, Latin1_General_CI_AS, *_BIN2_UTF8, Chinese_PRC_CI_AS, or Modern_Spanish_CI_AS. Many others will be supported by methods like Microsoft.SqlServer.Management.Smo.Server.GetStringComparer or have some collations that follow the Unicode rules for sorting and comparison.

    And at the end of the day, say we implemented this; this would mean that you can start using e.g. StringComparison.OrdinalIgnoreCase, and it would work assuming you've configured your column with the appropriate collation, and throw otherwise. Just like today, the dev would have to go and make sure the column collation is insensitive - that must happen either way.

    The dev really wouldn't have to go make sure of the column collation in many cases... if you know all your non-legacy databases are configured by default with a particular collation, you can just use that one and only worry about the specifics in special circumstances, with the added safety that you'll get an exception if you forget you are in a special circumstance.

    So the only value here is somewhat better discoverability of the fact that you've misconfigured your column collation when you get it wrong; it's not nothing, but I don't think it makes sense in the grand scheme of things.

    It's not just discoverability of getting it wrong, it is the ability to even run the code that does it right. Such as writing a comparison expression that will work the same across databases and LINQ to objects. (Though perhaps only if you're able to change all of them to similar enough collations.)

    Would you say it would be an objectionable feature for EF to have? Or a feature that would be ok, but not important enough to allocate resources to?

  20. roji commented on Jul 11, 2023

    @roji
    Member

    Does adding UseCollation to the model force you to use a migration, or will it currently be ignored if you don't run a migration? Since we're using DACPAC, not migrations, and we call UseCollation, I'm pretty sure it doesn't cause problems.

    Adding UseCollation doesn't force you to use a migration - EF generally doesn't force you to migrations; you give it a connection string and it connects to that database. If you're managing your database externally, it simply doesn't take effect on the column collation. UseCollation may still affect certain things in the query pipeline, but the column collation is what you defined it to be when creating the database.

    On projects I've been working on, we've usually used the command line tool to scaffold the context, since we were not usually using migrations. That already exports information about collations, at least for some providers.

    Not everyone does that.

    It's not just discoverability of getting it wrong, it is the ability to even run the code that does it right. Such as writing a comparison expression that will work the same across databases and LINQ to objects. (Though perhaps only if you're able to change all of them to similar enough collations.)

    The point is that if your databases are set up correctly, it makes zero difference whether you're using the StringComparison overload or the overload without StringComparison - you get the exact same behavior. The only thing that your proposal brings is for EF to throw when the database column is not set up correctly, in which case the dev would go and set it up correctly. That's why I can't see this being anything beyond a misconfiguration discoverability feature.

    Would you say it would be an objectionable feature for EF to have? Or a feature that would be ok, but not important enough to allocate resources to?

    First of all, EF never does the sort of "automatic schema inspection" this feature would require, i.e. to know the collations of all columns in your database in advance in case they are queried (or to get that information on-demand when some column is queried). Aside from the considerable work this would require, this will very likely be a problematic approach for many reasons - so I'd object to doing it, especially for the very little value this brings.

    And second, as I wrote above, since things really do not correspond between .NET and the database on this, I really do think it's a better design for the LINQ query to be completely agnostic of collation details. IMO the right way here is for the user to express that "the column should be compared to something" without specifying anything about the how. This promotes understanding of how things are actually working under the hood, i.e. that the how is fully specified on the database-side in the column collation.

  21. added theissue type on Jun 15, 2025
  22. marked Firstotdefault async error #15426 as a duplicate of this issue on Jun 16, 2025
  23. Thieum commented on Jul 7, 2025

    @Thieum

    String.Compare with 3 arguments doesn't seem to be supported either:

    This could lead to false positive with code analysis tools like CodeRush: https://docs.devexpress.com/CodeRushForRoslyn/401176/static-code-analysis/analyzers-library/crr0050-string-compare-can-be-used

  24. tanvirarjel commented on Nov 29, 2025

    @tanvirarjel

    11 Years! Still not fixed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions