如何在C#中按值获取字典键?
Dictionary<string, string> types = new Dictionary<string, string>()
{
{"1", "one"},
{"2", "two"},
{"3", "three"}
};
我想要这样的东西:
getByValueKey(string value);
getByValueKey("one")
必须返回"1"
。
最好的方法是什么?也许HashTable,SortedLists?
值不一定必须是唯一的,因此您必须进行查找。您可以执行以下操作:
var myKey = types.FirstOrDefault(x => x.Value == "one").Key;
如果值是唯一的,并且插入频率比读取的频率低,则创建一个反向字典,其中值是键,而键是值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句