我有几个字典,我想执行交集。 两个词典对于同一个键可能有不同的值列表。基于键和值的字典交集
实施例:
Dictionary<int, List<int>> Primary = new Dictionary<int, List<int>>();
Primary.Add(1, new List<int>());
Primary[1].Add(5);
Primary[1].Add(6);
Primary[1].Add(7);
Primary.Add(2, new List<int>());
Primary[2].Add(5);
Dictionary<int, List<int>> Secondary = new Dictionary<int, List<int>>();
Secondary.Add(1, new List<int>());
Secondary[1].Add(6);
Secondary[1].Add(7);
Secondary[1].Add(8);
Secondary.Add(3, new List<int>());
Secondary[3].Add(5);
因此,我希望得到的字典只包含:
[1],[表< 6,7>]
我怎样才能做到这一点?实际的数据结构,我想交叉口的形式为:
Dictionary<long, SortedList<int,List<long>>>
这是你的哪一部分挣扎?你有什么尝试? –
如果在两个字典中都存在关键字,但相应的列表中没有公共项目,您希望结果是否包含具有空列表的关键字? – jmcilhinney
@jmcilhinney是的,如果它们具有相同的键但列表中没有共同的项目,则它应该为空。 BartoszKP给出的答案完美适用于Dictionary>。我试图扩展Dictionary >>。它令人困惑 –
Sabz