SelectMany linq query on dictionary property
See original GitHub issueDescribe the bug
I think this may be a bug, but I’m not sure if what I’m doing is supported or if there is another work around.
I’m trying to query a dictionary property on an object using a ‘SelectMany’. I know this works on child arrays/lists on objects, but with my dictionary it’s just returning zero results.
To Reproduce
I have an object cosmosDb with a dictionary property:
public Dictionary<Guid, Response> Responses { get; set; } = new Dictionary<Guid, Response>();
I’m trying to do a ‘SelectMany’ linq query to get all the responses across my objects with a certain key e.g.
query.SelectMany(q => q.Responses.Where(r => r.Key == questionId)
.Select(r => r.Value));
Expected behavior/Actual behavior
In the above example I’d want a list of responses back, instead I get zero results.
Environment summary SDK Version: 3.3.0 OS Version Windows
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
For dictionary, can you access the element using the key instead of filtering by a Where clause? ie. query.Select(q => q.Responses[questionId]);
This is due to there is no direct SQL equivalent for the LINQ expression semantics (i.e. SelectMany and filter on the key in general or iterating on the properties of the object which represents the dictionary) and relative equivalent translation is not supported yet.
@khdang , was this ever revisited? I understand there is no “relative equivalent translation”, but creating a query instead that will always returns zero results instead of throwing a not supported exception seems like a pretty big flaw. I had to go investigate the underlying query and figure out why no results were coming back. I see how this “might” work if a custom serializer is used that translates dictionaries into collections instead of objects, but how it is now just feels like a big trap with unexpected results using the default serializers.