欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中查找Dictionary中重復(fù)值的方法

 更新時(shí)間:2014年01月15日 14:57:14   作者:  
這篇文章主要介紹了C#中查找Dictionary中重復(fù)值的方法,有需要的朋友可以參考一下

簡介

在這篇幫助文檔中,我將向你展示如何實(shí)現(xiàn)c#里字典中重復(fù)值的查找。你知道的對于一個(gè)老鳥來說,這是非常簡單的代碼。但是盡管如此,這也是一篇對c#初學(xué)者非常有用的幫助文檔。

背景

多數(shù)程序員對小型數(shù)據(jù)源存儲(chǔ)的處理方式通常是創(chuàng)建字典進(jìn)行鍵值存儲(chǔ)。主鍵時(shí)唯一的,但是字典值卻可能有重復(fù)的元素。

代碼

這里我使用了一個(gè)簡單的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)文章

最新評論