C # Dictionary Gets the corresponding key value by value [forwarding]

Source: Internet
Author: User

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]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.