度日值字典键 [英] get dictionary key by value

查看:240
本文介绍了度日值字典键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/255341/getting-key-of-value-of-a-generic-dictionary\">Getting的通用词典的价值关键?

我要如何通过值在C#中的字典键?

How do I get a Dictionary key by value in C#?

Dictionary<string, string> types = new Dictionary<string, string>()
{
            {"1", "one"},
            {"2", "two"},
            {"3", "three"}
};

我想是这样的:

getByValueKey(string value);

getByValueKey(1)必须返回1

什么是最好的方式做到这一点?也许哈希表,SortedLists?

What is the best way do this? Maybe HashTable, SortedLists?

推荐答案

值不一定必须是唯一的,所以你必须做一个查找。你可以做这样的事情:

Values not necessarily have to be unique so you have to do a lookup. You can do something like this:

var myKey = types.FirstOrDefault(x => x.Value == "one").Key;

如果值是唯一的,并且频率低于读取插入,然后建立逆字典,其中值的键和键的值。

If values are unique and are inserted less frequently than read, then create an inverse dictionary where values are keys and keys are values.

这篇关于度日值字典键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆