Given the following models:
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}"
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?
- depends on
-
CSHARP-1913 Support using Dictionary fields as IEnumerable<KeyValuePair<TKey, TValue>>
-
- In Code Review
-
- duplicates
-
CSHARP-1913 Support using Dictionary fields as IEnumerable<KeyValuePair<TKey, TValue>>
-
- In Code Review
-