Say I have this dictionary:
ConcurrentDictionary<string, Location> connections;
I want to get a list of the keys from this dictionary that match some conditions on their values. For example, this works for me:
List<string> users = connections.Where(x.Value.IsNearby(anotherLocation)).ToDictionary(x => x.Key, x => x.Value).Keys.ToList();
But this is nasty because it goes from a dictionary to sub-dictionary, to a list of keys. Is there a more elegant way of doing this?
It's not clear why you've got the ToDictionary
call at all. You just need a Select
call:
List<string> users = connections.Where(x => x.Value.IsNearby(anotherLocation)
.Select(x => x.Key)
.ToList();
See more on this question at Stackoverflow