Uploaded image for project: 'Realm .NET SDK'
  1. Realm .NET SDK
  2. RNET-902

[Bug]: Queries with string.Contains(string, StringComparison) fails on .NET 6+

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None

      What happened?

      In .NET 6 (or 7), Where queries which contains string.Contains(string, StringComparison) fails to execute by NotSupportedException.
      (See the code snippet)

      I found that it seems to be caused by a workaround introduced by #1854 and this seems not to suit with .NET's string.Compare instance method.

      I also wrote a fix of this bug, so I can submit a pull request if it can be accepted.

      Repro steps

      1. In a query for Realm, use string.Compare(string value, StringComparison comparisonType) instance method (see the code snippet)
        • The first argument (value) must not be literal. This issue only happens if the value is MemberExpression (closure variable satisfies this)
      2. Querying fails with System.NotSupportedException. (see the stacktrace)

      Version

      Realm 10.18.0 on .NET 7.0.101

      What SDK flavour are you using?

      Local Database only

      What type of application is this?

      Console/Server

      Client OS and version

      Windows (x64) 10.0.22623.1028

      Code snippets

      Unable to find source-code formatter for language: csharp. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      using Realms;
      class Klass : RealmObject
      {
          public string Text { get; set; }
      }
      
      class Program
      {
          static void Main()
          {
              var realm = Realm.GetInstance("foo.realm");
              realm.Write(() => realm.Add(new Klass { Text = "foobar" }));
              var str = "FOO"; // for reproduction, the first argument of Compare must not be literal
              foreach (var x in realm.All<Klass>().Where(x => x.Text.Contains(str, StringComparison.OrdinalIgnoreCase)))
                                                           // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <- crash!
                  Console.WriteLine(x.Text);
          }
      }
      

      Stacktrace of the exception/crash you're getting

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      Unhandled exception. System.NotSupportedException: The left-hand side of the Call operator must be a direct access to a persisted property in Realm.
      Unable to process 'value(Program+<>c__DisplayClass0_0).str'.
         at Realms.RealmResultsVisitor.GetColumnName(MemberExpression memberExpression, Nullable`1 parentType)
         at Realms.RealmResultsVisitor.VisitMethodCall(MethodCallExpression node)
         at Realms.RealmResultsVisitor.VisitMethodCall(MethodCallExpression node)
         at Realms.RealmResults`1.GetOrCreateHandle()
         at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
         at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
         at System.Lazy`1.CreateValue()
         at Realms.RealmCollectionBase`1.get_IsValid()
         at Realms.RealmCollectionBase`1.Enumerator..ctor(RealmCollectionBase`1 parent)
         at Realms.RealmCollectionBase`1.GetEnumerator()
         at Program.Main() in C:\w\ConsoleApp1\ConsoleApp1\Program.cs:line 15
      

      ```

      Relevant log output

      No response

            Assignee:
            nikola.irinchev@mongodb.com Nikola Irinchev
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: