If you have 2 dictionaries of the same datatype in C# which you want to merge together then you can use the following code.
Dictionary<string, string> primaryDictionary = new Dictionary<string, string>()
{
	{ "Number", "One" },
	{ "Food", "Carrot" },
	{ "Cake", "Chocolate" }
};
Dictionary<string, string> secondaryDictionary = new Dictionary<string, string>()
{
	{ "Number", "Two" },
	{ "Location", "London" }
};
primaryDictionary.ToList().ForEach(x => secondaryDictionary[x.Key] = x.Value);If a key exists in both of the dictionaries then the primary value will overwite the secondary value. In the above example secondaryDictionary will now contain 4 entries with the value of “Number” being set to “One”.
0 Comments