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

Support Dictionary.ContainsValue in LINQ queries

    • Type: Icon: New Feature New Feature
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.26.0
    • Affects Version/s: None
    • Component/s: Linq, LINQ3
    • Fully Compatible
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Tested using version 2.7.3 of the C# driver.

      Issue appears to be in MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate. 

      Given the following document:

      {
          "_id" : ObjectId("5c5c8d915018e66f49cf8b59"),
          "UserAttributes" : {
              "CustomerClass" : "Schools",
              "CustomerSubClass" : "Law Schools"
          },
          "UserId" : "urn:idprofile:PA10000"
      } 

       

      this code will fail:

      public class User
      {
       public ObjectId _id;
       public string UserId { get; set; }
      public Dictionary<string, string> UserAttributes = new Dictionary<string, string>();
      }
      var uri = "xxx";
      var mongoURL = new MongoUrl(uri);
      var client = new MongoClient(mongoURL);
      var database = client.GetDatabase("csharp");
      var Users = database.GetCollection<User>("Users");
      int count = Users.AsQueryable().Where(x => x.UserAttributes.ContainsValue("Law Schools")).Count(); 
      

       

      The error that is produced:

      System.ArgumentException HResult=0x80070057 Message=Unsupported filter: \{document}{UserAttributes}.ContainsValue("Law Schools"). Source=MongoDB.Driver StackTrace: at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateWhere(WhereExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateGroupBy(GroupByExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions) at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Translate(Expression expression) at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression) at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute[TResult](Expression expression) at System.Linq.Queryable.Count[TSource](IQueryable`1 source) at ConsoleApp1.Program.Test2() in C:\Workspace\ConsoleApp1\ConsoleApp1\Program.cs:line 77 at ConsoleApp1.Program.Main(String[] args) in C:\Workspace\ConsoleApp1\ConsoleApp1\Program.cs:line 31
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            alex.bevilacqua@mongodb.com Alex Bevilacqua
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: