比如说Dictionary<string, string> openWith =
new Dictionary<string, string>();openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");那我能通过value值比如说“notepad.exe”找到key:“txt”么?
new Dictionary<string, string>();openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");那我能通过value值比如说“notepad.exe”找到key:“txt”么?
foreach(var kv in dict)
{
if (kv.Value == "notepad.exe")
{
findKey = kv.Key;
break;
}
}
openWith.Add("dib", "paint.exe")相同的value,你希望返回哪个key呢?BTW: 上面的Dictionary,如果用linq写起来更简洁些:
var key = dict.First(kv => kv.Value == "notepad.exe").Key;
static IEnumerable<TKey> FindKeysFromValue<TKey, TValue>(IDictionary<TKey, TValue> dic, TValue value)
{
if (dic == null)
return Enumerable.Empty<TKey>();
return dic.TakeWhile(pair => pair.Value.Equals(value)).Select(pair => pair.Key);
}
得到字典中的以“WA”为键的值
statecaps.TryGetValue("WA",dstr)