CSharp使用Dictionary_GetValueOrGetKey
Dictionary<string, string> map = new Dictionary<string, string>();
map.Add(“1”, “1”);
map.Add(“2”, “2”);
map.Add(“3”, “2”);
1.foreach KeyValuePair traversing
1 2 3 4 5 6 7
| foreach (KeyValuePair<string, string> e in map) { if (e.Value.Equals("2")) { } }
|
2.foreach map.Keys
1 2 3 4 5 6 7
| foreach (string key in map.Keys) { if (map[key].Equals("2")) { } }
|
3.Linq
1 2
| //get all keys var keys = map.Where(x => x.Value == "2").Select(y => y.Key);
|
4.Get first key
1
| var firstKey = map.FirstOrDefault(x => x.Value == "2").Key;
|