在這篇幫助文檔中,我將向你展示如何實現(xiàn)c#里字典中重復(fù)值的查找。你知道的對于一個老鳥來說,這是非常簡單的代碼。但是盡管如此,這也是一篇對c#初學(xué)者非常有用的幫助文檔。
背景:多數(shù)程序員對小型數(shù)據(jù)源存儲的處理方式通常是創(chuàng)建字典進行鍵值存儲。主鍵時唯一的,但是字典值卻可能有重復(fù)的元素。
代碼如下
//initialize a dictionary with keys and values. Dictionary<int, string> plants = new Dictionary<int, string>() { {1,"Speckled Alder"}, {2,"Apple of Sodom"}, {3,"Hairy Bittercress"}, {4,"Pennsylvania Blackberry"}, {5,"Apple of Sodom"}, {6,"Water Birch"}, {7,"Meadow Cabbage"}, {8,"Water Birch"} }; Response.Write("<b>dictionary elements........ m.survivalescaperooms.com </b><br />"); //loop dictionary all elements foreach (KeyValuePair<int, string> pair in plants) { Response.Write(pair.Key + "....."+ pair.Value+"<br />");} //find dictionary duplicate values. var duplicateValues = plants.GroupBy(x => x.Value).Where(x => x.Count() > 1); Response.Write("<br /><b>dictionary duplicate values..........</b><br />"); //loop dictionary duplicate values only foreach(var item in duplicateValues) { Response.Write(item.Key+"<br />");}以上就是我使用一個簡單的LINQ語句來查找字典中的重復(fù)值,大家可以嘗試實驗一下。
新聞熱點
疑難解答
圖片精選