[CSHARP-4251] Projecting dictionaries (to lists) through IQueryable. Created: 12/Jul/22  Updated: 27/Oct/22

Status: Backlog
Project: C# Driver
Component/s: LINQ3
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Unknown
Reporter: stijn tallon Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: Queryable, projection
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

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?



 Comments   
Comment by James Kovacs [ 15/Jul/22 ]

Hi, stijn.tallon@gmail.com,

Thank you for reaching out to us with your question. Neither the LINQ2 nor LINQ3 providers support the ability to project a Dictionary<K,V>.Items into a List<V>. We will consider implementing this feature in our new LINQ provider, LINQ3. Please follow this ticket for updates.

Sincerely,
James

Generated at Wed Feb 07 21:47:41 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.