В моем коде я имею линиюСловарь Союза словарь?
var d3 = d.Union(d2).ToDictionary(s => s.Key, s => s.Value);
Это чувствует себя странно, потому что его, вероятно, я буду делать это много. Нет .ToDictionary()
. Как объединить словарь и сохранить его в качестве словаря?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var list = new List<Tuple<int, string>>();
list.Add(new Tuple<int, string>(1, "a"));
list.Add(new Tuple<int, string>(3, "b"));
list.Add(new Tuple<int, string>(9, "c"));
var d = list.ToDictionary(
s => s.Item1,
s => s.Item2);
list.RemoveAt(2);
var d2 = list.ToDictionary(
s => s.Item1,
s => s.Item2);
d2[5] = "z";
var d3 = d.Union(d2).ToDictionary(s => s.Key, s => s.Value);
}
}
}
Это может помочь: http://stackoverflow.com/questions/294138/merging-dictionaries-in-c-sharp – sgeddes