相关疑难解决方法(0)

使用Enumerable.Union方法合并字典<TKey,TValue>

我正在测试UNION方法以合并到字典(类型为Dictionary).它适用于TValue类型是字符串或int甚至对象.但是如果TValue类型是一个集合(使用List和object []测试),则抛出异常:"ArgumentException:已经添加了具有相同键的项目."

这是我的代码:

Dictionary<int,string> _dico1 = new Dictionary<int, string>()
{
    {0, "zero"},
    {1, "one"}
};

Dictionary<int,string> _dico2 = new Dictionary<int,string>()
{
    {1 , "one"},
    {2 , "two"},
    {3 , "three"},
    {4 , "four"},
    {5 , "five"},
    {6 , "six"}
};

Dictionary<int, List<string>> _dico3 = new Dictionary<int, List<string>>()
{
    {0, new List<string>{"zero"}},
    {1, new List<string>{"one"}}
};

Dictionary<int, List<string>> _dico4 = new Dictionary<int, List<string>>()
{
    {1, new List<string>{"one"}},
    {2, new List<string>{"two"}},
    {3, new List<string>{"three"}},
    {4, new List<string>{"four"}},
    {5, new …
Run Code Online (Sandbox Code Playgroud)

c# union merge dictionary

7
推荐指数
1
解决办法
4916
查看次数

标签 统计

c# ×1

dictionary ×1

merge ×1

union ×1