Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么两本字典的结合不起作用?_C#_Linq - Fatal编程技术网

C# 为什么两本字典的结合不起作用?

C# 为什么两本字典的结合不起作用?,c#,linq,C#,Linq,代码: 似乎我缺少显式强制转换?第二个是字典,而联合的返回类型是IEnumerable。您需要将其转换回字典 Dictionary<string, IList<PuntoMappa>> first = new Dictionary<string, IList<PuntoMappa>>(); Dictionary<string, IList<PuntoMappa>> second = new Dictionar

代码:

似乎我缺少显式强制转换?

第二个是字典,而联合的返回类型是IEnumerable。您需要将其转换回字典

    Dictionary<string, IList<PuntoMappa>> first = new Dictionary<string, IList<PuntoMappa>>();
    Dictionary<string, IList<PuntoMappa>> second = new Dictionary<string, IList<PuntoMappa>>();
    second = second.Union(first);
正如注释所指出的,如果您有重复的密钥,这可能不起作用。如果您想在两个词典中使用匹配键合并列表,则可以使用联接:

第二个是字典,而您的联合的返回类型是IEnumerable。您需要将其转换回字典

    Dictionary<string, IList<PuntoMappa>> first = new Dictionary<string, IList<PuntoMappa>>();
    Dictionary<string, IList<PuntoMappa>> second = new Dictionary<string, IList<PuntoMappa>>();
    second = second.Union(first);
正如注释所指出的,如果您有重复的密钥,这可能不起作用。如果您想在两个词典中使用匹配键合并列表,则可以使用联接:


Union返回IEnumerable,而不是dictionary

    Dictionary<string, IList<PuntoMappa>> first = new Dictionary<string, IList<PuntoMappa>>();
    Dictionary<string, IList<PuntoMappa>> second = new Dictionary<string, IList<PuntoMappa>>();
    second = second.Union(first);
如果需要IEnumerable,则需要一个新变量:

second = first.Join(second, kvp => kvp.Key, kvp => kvp.Key, (pair1, pair2) => new { Name = pair1.Key, PuntoMappas = pair1.Value.Union(pair2.Value) })
  .ToDictionary(p => p.Name, p => p.PuntoMappas.ToList());
或-要创建新词典,请执行以下操作:

var newEnumerable = second.Union(first);

Union返回IEnumerable,而不是dictionary

    Dictionary<string, IList<PuntoMappa>> first = new Dictionary<string, IList<PuntoMappa>>();
    Dictionary<string, IList<PuntoMappa>> second = new Dictionary<string, IList<PuntoMappa>>();
    second = second.Union(first);
如果需要IEnumerable,则需要一个新变量:

second = first.Join(second, kvp => kvp.Key, kvp => kvp.Key, (pair1, pair2) => new { Name = pair1.Key, PuntoMappas = pair1.Value.Union(pair2.Value) })
  .ToDictionary(p => p.Name, p => p.PuntoMappas.ToList());
或-要创建新词典,请执行以下操作:

var newEnumerable = second.Union(first);

Union将你的两本词典视为IEnumerableUnion将你的两本词典视为IEnumerabley你没有解释你的问题是什么。你看到错误了吗?你看到了意想不到的结果吗?你没有解释你的问题是什么。你看到错误了吗?你看到了意想不到的结果吗?