C#中查找Dictionary中的反復值的辦法。本站提示廣大學習愛好者:(C#中查找Dictionary中的反復值的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中查找Dictionary中的反復值的辦法正文
在這篇贊助文檔中,我將向你展現若何完成c#裡字典中反復值的查找。你曉得的關於一個老鳥來講,這長短常簡略的代碼。然則雖然如斯,這也是一篇對c#初學者異常有效的贊助文檔。
配景:多半法式員對小型數據源存儲的處置方法平日是創立字典停止鍵值存儲。主鍵時獨一的,然則字典值卻能夠有反復的元素。
代碼以下
//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........ www.jb51.net </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語句來查找字典中的反復值,年夜家可以測驗考試試驗一下。