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

Projecting dictionaries (to lists) through IQueryable.

    • Type: Icon: New Feature New Feature
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: LINQ3
    • 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?

      Given the following models:

      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
      public class DatabaseObject
      {
          public Dictionary<string, DatabaseValueObject> Items { get; set; } = new Dictionary<string, DatabaseValueObject>();
      }
      public class DatabaseValueObject
      {
          public string Description { get; set; } = string.Empty;
          public int Count { get; set; }
      }
      
      public class MappedObject
      {
          public List<KeyValuedObject> Items { get; set; } = new List<KeyValuedObject>();
      }
      public class KeyValuedObject
      {
          public string Name { get; set; } = string.Empty;
          public int Count { get; set; }
          public string Description { get; set; } = string.Empty;
      }
      

      We'd like to project the dictionary into a list through linq, however any operation on the .Items results in the exception "The exrpession tree is not supported:

      {document} {Items}

      "

      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
      var client = new MongoClient(connectionstring);
      
      var queryable = client.GetDatabase("test").GetCollection<DatabaseObject>("test_collection").AsQueryable();
       
      queryable.Select(n => new MappedObject {
        Items = n.Items.Select(k => new KeyValuedObject(){ Count = k.Value.Count, Description = k.Value.Description, Name = k.Key }).ToList()
       }).Dump("Manual projection");
      

      The goal was to have this mapping be done through automapper, which generates a nearly identical query to the manual one above. But since any operation (select, tolist, ..) on the .Items object results in an error, we can't use projections at all.

      It might be that projections on subdocuments like this aren't supported at all on the server side if the dictionary keys can't be listed from the client?

            Assignee:
            Unassigned Unassigned
            Reporter:
            stijn.tallon@gmail.com stijn tallon
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: