首页 文章

按值获取字典键

提问于
浏览
294

如何在C#中按值获取Dictionary键?

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

我想要这样的东西:

getByValueKey(string value);

getByValueKey("one") 必须返回 "1" .

这样做的最佳方式是什么?也许是HashTable,SortedLists?

7 回答

  • -11

    值不一定必须是唯一的,因此您必须进行查找 . 你可以这样做:

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

    如果值是唯一的并且插入频率低于读取值,则创建一个逆字典,其中值是键,键是值 .

  • 525

    你可以这样做:

    • 循环遍历字典中的所有 KeyValuePair<TKey, TValue> (如果字典中有许多条目,这将是一个相当大的性能影响)

    • 使用两个词典,一个用于值到键映射,另一个用于键到值映射(这将占用内存空间的两倍) .

    如果不考虑性能,请使用方法1,如果不考虑内存,请使用方法2 .

    此外,所有键必须是唯一的,但这些值不必是唯一的 . 您可能有多个具有指定值的键 .

    你有什么理由不能扭转关键 Value 关系吗?

  • -1

    我遇到了Linq绑定不可用的情况,并且必须明确地扩展lambda . 它产生了一个简单的功能:

    public static string KeyByValue(Dictionary<string, string> dict, string val)
    {
        string key = null;
        foreach (KeyValuePair<string, string> pair in dict)
        {
            if (pair.Value == val)
            { 
                key = pair.Key; 
                break; 
            }
        }
        return key;
    }
    

    称之为:

    public static void Main()
    {
        Dictionary<string, string> dict = new Dictionary<string, string>()
        {
            {"1", "one"},
            {"2", "two"},
            {"3", "three"}
        };
    
        string key = KeyByValue(dict, "two");       
        Console.WriteLine("Key: " + key);
    }
    

    适用于.NET 2.0和其他有限的环境 .

  • -2

    也许是这样的:

    foreach (var keyvaluepair in dict)
    {
        if(Object.ReferenceEquals(keyvaluepair.Value, searchedObject))
        {
            //dict.Remove(keyvaluepair.Key);
            break;
        }
    }
    
  • 2

    以下代码仅在包含唯一值数据时有效

    public string getKey(string Value)
    {
        if (dictionary.ContainsValue(Value))
        {
            var ListValueData=new List<string>();
            var ListKeyData = new List<string>();
    
            var Values = dictionary.Values;
            var Keys = dictionary.Keys;
    
            foreach (var item in Values)
            {
                ListValueData.Add(item);
            }
    
            var ValueIndex = ListValueData.IndexOf(Value);
            foreach (var item in Keys)
            {
                ListKeyData.Add(item);
            }
    
            return  ListKeyData[ValueIndex];
    
        }
        return string.Empty;
    }
    
  • -3
    types.Values.ToList().IndexOf("one");
    

    Values.ToList()将字典值转换为对象列表 . IndexOf(“one”)搜索新列表,查找“one”并返回与字典中键/值对的索引匹配的索引 .

    此方法不关心字典键,它只返回您要查找的值的索引 .

    请记住,字典中可能有多个“one”值 . 这就是没有“获取关键”方法的原因 .

  • 23

    我有非常简单的方法来做到这一点 . 它对我来说非常完美 .

    Dictionary<string, string> types = new Dictionary<string, string>();
    
    types.Add("1", "one");
    types.Add("2", "two");
    types.Add("3", "three");
    
    Console.WriteLine("Please type a key to show its value: ");
    string rLine = Console.ReadLine();
    
    if(types.ContainsKey(rLine))
    {
        string value_For_Key = types[rLine];
        Console.WriteLine("Value for " + rLine + " is" + value_For_Key);
    }
    

相关问题