在N组Key,Value 的字典中取交集

guotongyu1 2014-09-01 11:52:07
数据结构式这样

Key,Value
A,1
A,2
A,3
A,4
B,2
B,3
B,5
C,1
C,4
C,5
D,3
.....

我要取计算出这几组Key中 两两Key 为一组的value相同数量有几个。
结果是这样
a,b =2
a,c = 2
a,d =1
b,c = 1
b,d= 1
c,d = 0
...全文
111 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2014-09-01
  • 打赏
  • 举报
回复
List<KeyValuePair<string, int>> list = new List<KeyValuePair<string, int>>();
list.Add(new KeyValuePair<string, int>("A, 1"));
...
var query = list.SelectMany(x => list.Where(y => y.Key > x.Key && y.Value == x.Value).Select(y => new { x, y }))
    .GroupBy(x => new { k1 = x.Key, k2 = y.Key })
    .Select(x => string.Format("{0},{1} = {2}", x.Key.k1, x.Key.k2, x.Count()));
foreach (string item in query)
    Console.WriteLine(item);
  • 打赏
  • 举报
回复
简单粗暴的方式丢到若干个list里 然后彼此做交集 求楼下给出更好的解决方案

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧