1: The most straightforward loop traversal method, can be divided into the traversal Key--value key value pairs and all key two forms of expression
2: Query in Linq (of course, add the corresponding namespace using System.Linq)
Here's a very simple code example:
Private voidGetdickeybyvalue () {Dictionary<string,string> dic =Newdictionary<string,string>(); Dic. ADD ("1","1"); Dic. ADD ("2","2"); Dic. ADD ("3","2"); //foreach KeyValuePair traversing foreach(keyvaluepair<string,string> kvpinchdic) {if(KVP. Value.equals ("2")) { //... kvp. Key; } } //foreach dic. Keys foreach(stringKeyinchDiC. Keys) {if(Dic[key]. Equals ("2")) { //... key } } //Linq varKeys = dic. Where (q = Q.value = ="2"). Select (q = q.key);//Get all keysList<string> keylist = ( fromQinchDiCwhereQ.value = ="2" SelectQ.key). tolist<string> ();//Get all keys varFirstkey = dic. FirstOrDefault (q = Q.value = ="2"). Key;//get first key}
Original address: http://www.cnblogs.com/mingmingruyuedlut/archive/2013/05/23/3067227.html
C # Dictionary Gets the corresponding key value by value [forwarding]