question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SelectMany linq query on dictionary property

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
khdangcommented, Oct 15, 2019

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.

0reactions
Matthewsrecommented, Dec 3, 2022

@coffeymatt Sure, I’m glad that it works for you. I’m closing the issue and will take a note to revisit it. Thanks.

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - SelectMany to Dictionary problems
I am struggling with a Linq statement to get get a list of items from within a list of items and convert to...
Read more >
Enumerable.SelectMany Method (System.Linq)
The immediate return value is an object that stores all the information that is required to perform the action. The query represented by...
Read more >
Using C# linq fetch records into dictionary
Something like this should work: C#. Dictionary<string, List<int>> setsGroups = dct .GroupBy(kvp => kvp.Value) .ToDictionary(grp => grp.
Read more >
LINQ ToDictionary() Method
In LINQ, the ToDictionary operator is useful to convert list/collection (IEnumerable<T>) items to a new dictionary object (Dictionary<TKey, TValue>), ...
Read more >
c# - Need help finding an efficient way to group the keys in ...
The LINQ query I wrote for this is: Dictionary<int, List<int>> test = <Some init data set>; test.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found