C#中查找Dictionary中重復(fù)值的方法
更新時間:2014年01月15日 14:57:14 作者:
這篇文章主要介紹了C#中查找Dictionary中重復(fù)值的方法,有需要的朋友可以參考一下
簡介
在這篇幫助文檔中,我將向你展示如何實(shí)現(xiàn)c#里字典中重復(fù)值的查找。你知道的對于一個老鳥來說,這是非常簡單的代碼。但是盡管如此,這也是一篇對c#初學(xué)者非常有用的幫助文檔。
背景
多數(shù)程序員對小型數(shù)據(jù)源存儲的處理方式通常是創(chuàng)建字典進(jìn)行鍵值存儲。主鍵時唯一的,但是字典值卻可能有重復(fù)的元素。
代碼
這里我使用了一個簡單的LINQ語句來查找字典中的重復(fù)值。
復(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........ www.dbjr.com.cn </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 />");
}
您可能感興趣的文章:
相關(guān)文章
使用位運(yùn)算實(shí)現(xiàn)網(wǎng)頁中的過濾、篩選功能實(shí)例
這篇文章主要介紹了使用位運(yùn)算實(shí)現(xiàn)網(wǎng)頁中的過濾、篩選功能實(shí)例,一個比常規(guī)拼接SQL字符串更有新意的一個解決思路,需要的朋友可以參考下2014-07-07WPF實(shí)現(xiàn)背景燈光隨鼠標(biāo)閃動效果
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)背景燈光隨鼠標(biāo)閃動效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08