I want to get the key of my value but this does not Possible in Hashtable
Is there data stretcher to do this ??
Hashtable x = new Hashtable();
x.Add("1", "10");
x.Add("2", "20");
x.Add("3", "30");
x.GetKey(20);//equal 2
If all your keys are strings.
var key = x.Keys.OfType<String>().FirstOrDefault(s => x[s] == "20")
or better use a Dictionary instead:
Dictionary<string, string> x = new Dictionary<string, string>();
x.Add("1", "10");
x.Add("2", "20");
x.Add("3", "30");
string result = x.Keys.FirstOrDefault(s => x[s] == "20");
If you know that your value will always have just one distinct key, use Single
instead of FirstOrDefault
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With