Dictionaries do not work like this, nor are they intended to. How would you resolve the following:
key = "1", value = "x",
key = "2", value = "x"
You could do this:
var keys = dict.Where(kvp => kvp.Value == someValue).Select(kvp => kvp.Key);
foreach(var key in keys) {
Console.WriteLine(key);
}
But if you really need to go back and forth between keys and values you should consider encapsulating the problem into a two-way map. Here's a very simple implementation of this that you can tailor to your needs:
class TwoWayDictionary<TLeft, TRight> {
IDictionary<TLeft, TRight> leftToRight = new Dictionary<TLeft, TRight>();
IDictionary<TRight, TLeft> rightToLeft = new Dictionary<TRight, TLeft>();
public void Add(TLeft left, TRight right) {
if (leftToRight.ContainsKey(left)) {
throw new InvalidOperationException("key left is duplicate");
}
if (rightToLeft.ContainsKey(right)) {
throw new InvalidOperationException("key right is duplicate");
}
leftToRight.Add(left, right);
rightToLeft.Add(right, left);
}
public bool TryGetRightByLeft(TLeft left, out TRight right) {
return leftToRight.TryGetValue(left, out right);
}
public bool TryGetLeftByRight(out TLeft left, TRight right) {
return rightToLeft.TryGetValue(right, out left);
}
}
Note that this assumes that no key is ever duplicated.
Now you can say:
TwoWayDictionary<string, string> dict = new TwoWayDictionary<string, string>();
dict.Add("127.0.0.1", "localhost");
string host;
dict.TryGetRightByLeft("127.0.0.1", out host);
// host is "localhost"
string ip;
dict.TryGetLeftByRight("localhost", out ip);
// ip is "127.0.0.1"