के लिए आसान तरीका है मूल्य द्वारा शब्दकोश से कुंजी प्राप्त करने के लिए निम्न एक्सटेंशन विधि का उपयोग कर सकते हैं एक कुंजी:
public static TKey GetKey<TKey,TValue>(Dictionary<TKey, TValue> dictionary, TValue Value)
{
List<TKey> KeyList = new List<TKey>(dictionary.Keys);
foreach (TKey key in KeyList)
if (dictionary[key].Equals(Value))
return key;
throw new KeyNotFoundException();
}
और गुणकों चाबी के लिए:
public static TKey[] GetKeys<TKey, TValue>(Dictionary<TKey, TValue> dictionary, TValue Value)
{
List<TKey> KeyList = new List<TKey>(dictionary.Keys);
List<TKey> FoundKeys = new List<TKey>();
foreach (TKey key in KeyList)
if (dictionary[key].Equals(Value))
FoundKeys.Add(key);
if (FoundKeys.Count > 0)
return FoundKeys.ToArray();
throw new KeyNotFoundException();
}
[सामान्य जेनेरिक शब्दकोश के मूल्य की कुंजी प्राप्त करना] के संभावित डुप्लिकेट? (Http://stackoverflow.com/questions/255341/getting-key-of-value-of-a-generic-dictionary) – bluish