查找泛型集合中重复的数据并提示

.Net技术 码拜 8年前 (2016-09-19) 1240次浏览
Dictionary<int, int>  abc
abc.add[1,1]
abc.add[2,2]
abc.add[3,1]
abc.add[4,3]
abc.add[5,1]
abc.add[6,4]
abc的key1-6 这个是没有重复的,怎么样判断 abc.value的重复值,
每次点击but提示出 value的对应项key重复 例如:  1和3和5 是重复的,注:每次点击but只能提示一个项重复(第1个key),再点击则提示下一项重复(第3个key), 然后是第5的key, 再点击则从再次循环。
判断重复值是实现了,但是不知道怎么样实现每次点击循环提示
var valueDup = from item in abc.Values
group item by item into valueGroup
where valueGroup.Count() > 1
select valueGroup;
foreach (var item in valueDup)
{
MessageBox.Show(“重复的key是:” + item.Key.ToString());
}

请高手能给个提示,或给段代码。谢谢

解决方案

5

没有测试过,差不多就是这么回事。

Dictionary<int, int> abc = new Dictionary<int, int>();
方法外全局变量 Count=0;
            abc.Add(1, 1);
            abc.Add(2, 2);
            abc.Add(3, 1);
            abc.Add(4, 3);
            abc.Add(5, 1);
            abc.Add(6, 4);
            var q = abc.GroupBy(x => x.Value).Where(x=>x.Count()>1).Select(x => new { Key=x.Key, Count=x.Count() }).ToList();
Count=q-Count;
           for(int i=0;i<Count;i++){Console.WriteLine(q[i].Key+"--"+q[i].Count);Count-i;}

15

List<xxx> list;
int i=0;//这两个是全局变量
你执行linq的那段代码方法()
{
list= valueDup.ToList();
i=0;
}
点击事件()
{
if(list.Count-1>i)
{
var item = list[i];
MessageBox.Show("重复的key是:" + item.Key.ToString());    
i++;
}
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明查找泛型集合中重复的数据并提示
喜欢 (0)
[1034331897@qq.com]
分享 (0)