Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-4311

LINQ translation bug when document class derives from Dictionary<string, object>

    • Type: Icon: Question Question
    • Resolution: Gone away
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None

      Summary

      When the document class derives from Dictionary<string, object> and additionally has a property that is backed by a dictionary entry, LINQ does not work properly.

      Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

      MongoDB.Driver 2.17.1 on .NET 6

      How to Reproduce

      Consider following document class:

      public class MyType: Dictionary<string, object>
      {{{}}
          [BsonElement("displayname")]
          public string DisplayName
          {
              get => (string)this["displayname"];
              set => this["displayname"] = value;
      {{    }}}
      }

       

      Now if we try to do a LINQ query like 

      await collection.AsQueryable()
                  .Where(r => r.DisplayName == "Test")
                  .ToListAsync();

      we get an InvalidOperationException "The operands for operator 'Equal' do not match the parameters of method 'op_Equality'."

      Setting the LinqProvider to LinqProvider.V3 eliminates the exception, but the resulting list is always empty.

      Using .Where(r => r["displayname"] == "Test") results in a correct non-empty result list.

       

            Assignee:
            james.kovacs@mongodb.com James Kovacs
            Reporter:
            mywyb2@gmail.com Mo B.
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: