C#中查找Dictionary中的重复值的方法
在这篇帮助文档中,我将向你展示如何实现c#里字典中重复值的查找。你知道的对于一个老鸟来说,这是非常简单的代码。但是尽管如此,这也是一篇对c#初学者非常有用的帮助文档。
背景:多数程序员对小型数据源存储的处理方式通常是创建字典进行键值存储。主键时唯一的,但是字典值却可能有重复的元素。
代码如下
//initializeadictionarywithkeysandvalues.
Dictionary<int,string>plants=newDictionary<int,string>(){
{1,"SpeckledAlder"},
{2,"AppleofSodom"},
{3,"HairyBittercress"},
{4,"PennsylvaniaBlackberry"},
{5,"AppleofSodom"},
{6,"WaterBirch"},
{7,"MeadowCabbage"},
{8,"WaterBirch"}
};
Response.Write("<b>dictionaryelements........www.nhooo.com</b><br/>");
//loopdictionaryallelements
foreach(KeyValuePair<int,string>pairinplants)
{
Response.Write(pair.Key+"....."+pair.Value+"<br/>");
}
//finddictionaryduplicatevalues.
varduplicateValues=plants.GroupBy(x=>x.Value).Where(x=>x.Count()>1);
Response.Write("<br/><b>dictionaryduplicatevalues..........</b><br/>");
//loopdictionaryduplicatevaluesonly
foreach(variteminduplicateValues)
{
Response.Write(item.Key+"<br/>");
}
以上就是我使用一个简单的LINQ语句来查找字典中的重复值,大家可以尝试实验一下。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下