我有两个带有类型签名的排序词典
即
SortedDictionary<decimal, long> A
SortedDictionary<decimal, long> B
Run Code Online (Sandbox Code Playgroud)
我想合并密钥相同的两个列表,从而创建一个新的列表
SortedDictionary<decimal, KeyValuePair<long,long>>
or
SortedDictionary<decimal, List<long>>
Run Code Online (Sandbox Code Playgroud)
这可能不是解决问题的最好方法,但有人可以让我了解如何做到这一点或更好的方法来解决它.
这就是我所拥有的:
SortedDictionary<decimal, List<long>> merged = new SortedDictionary<decimal, List<long>>
(
A.Union(B)
.ToLookup(x => x.Key, x => x.Value)
.ToDictionary(x => x.Key, x => new List<long>(x))
);
Run Code Online (Sandbox Code Playgroud)
编辑:上述解决方案选择两个集合中未包含的密钥.这应该选择键相同的位置:
SortedDictionary<decimal, List<long>> merged = new SortedDictionary<decimal, List<long>>
(
A.Where(x=>B.ContainsKey(x.Key))
.ToDictionary(x => x.Key, x => new List<long>(){x.Value, B[x.Key]})
);
Run Code Online (Sandbox Code Playgroud)