There are times when you want to merge 2 dictionary collections to one . Below is a code sample that shows how to do it.
How to Merge Dictionary in C# ?
using System; using System.Collections.Generic; using System.IO; using System.Linq; namespace AbundantcodeConsoleApp { class Program { static void Main(string[] args) { Dictionary<int, string> dictionary1 = new Dictionary<int, string>(); dictionary1.Add(1, "item 1"); dictionary1.Add(2, "item 2"); Dictionary<int, string> dictionary2 = new Dictionary<int, string>(); dictionary2.Add(1, "item 1"); dictionary2.Add(4, "item 4"); var result = dictionary1.Concat(dictionary2).GroupBy(d => d.Key) .ToDictionary(d => d.Key, d => d.First().Value); Console.ReadLine(); } } }
1 Comment