获取包含值x的Dictionary中的所有键

Elm*_*lmo 15 .net c# vb.net dictionary

我有这个:

Dictionary<integer, string> dict = new Dictionary<integer, string>();
Run Code Online (Sandbox Code Playgroud)

我想选择字典中包含该值的所有项目abc.

是否有内置功能可以让我轻松完成这项工作?

Jon*_*eet 39

那么LINQ 的相当简单:

var matches = dict.Where(pair => pair.Value == "abc")
                  .Select(pair => pair.Key);
Run Code Online (Sandbox Code Playgroud)

请注意,这甚至不会有一点效率 - 这是一个O(N)操作,因为它需要检查每个条目.

如果您需要经常这样做,您可能需要考虑使用其他数据结构 - Dictionary<,>专门用于按键快速查找.