字典到条件匹配的键列表

时间:2014-08-10 16:51:16

标签: c# .net list dictionary type-conversion

说我有这本词典:

ConcurrentDictionary<string, Location> connections;

我想从这个字典中获取一个与其值相匹配的键的列表。例如,这对我有用:

List<string> users = connections.Where(x.Value.IsNearby(anotherLocation)).ToDictionary(x => x.Key, x => x.Value).Keys.ToList();

但这很讨厌,因为它从字典到子字典,再到键列表。有更优雅的方式吗?

1 个答案:

答案 0 :(得分:2)

不清楚为什么你完全接到ToDictionary电话。您只需要Select来电:

List<string> users = connections.Where(x => x.Value.IsNearby(anotherLocation)
                                .Select(x => x.Key)
                                .ToList();