Combining Multiple dictionary into one

I’m looking for a way to add data1,data2, and data3 into allData. Is this possible?

   List<Dictionary<string, object>> allData;

        List<Dictionary<string, object>> data1;
        List<Dictionary<string, object>> data2;
        List<Dictionary<string, object>> data3;

Sorry but we can not answer this question because it’s lacking a lot of vital information. How do all these lists with their dictionaries relate to each other? Do you have the exact same amount of dictionaries in each list? And if so, is there a 1-to-1 relationship between the indices of each list? So data1[0], data2[0], data3[0] and allData[0] belong together?

Even if what I said is true (so all lists contain the exact same amount of dictionaries and their relationship is 1-to-1) what do you want to happen when a key exists in two or more of the corresponding dictionaries? A dictionary can have each key only once. So naturally in such cases you would loose one of the objects. Maybe you somehow want to merge the data behind those keys. However since you have a dictionary of “object” we have absolutely no idea what you actually store in those.

So assuming:

  1. data1, data2, data3 and allData have the exact same amount of elements
  2. allData does not yet contain any data but should only contain the merged data of 1, 2 and 3
  3. They list elements have a 1-to-1 relationship
  4. There are no duplicate keys in those corresponding dictionaries

you can do this:

int count = data1.Count;
if (data2.Count != count || data3.Count != count)
    throw new System.Exception("The 3 Lists do not have the same amount of dictionaries");
allData = new List<Dictionary<string, object>>(count);
for(int i = 0; i < count; i++)
    var dict = new Dictionary<string, object>();
    foreach(var kv in data1*)*

dict.Add(kv.Key, kv.Value);
foreach(var kv in data2*)*
dict.Add(kv.Key, kv.Value);
foreach(var kv in data3*)*
dict.Add(kv.Key, kv.Value);
Note this code would throw a duplicate key exception if a certain key does already exist in one of the allData dictionaries. You can replace the dict.Add(kv.Key, kv.Value); with dict[kv.Key] = kv.Value; and it would no longer throw an error, but in this case it would overwrite existing keys. Since we process data1 first followed by data2 and data3, if there are duplicate keys, the one from the last processed dictionaries would end up in the allData dictionaries.
Note that I used many assumptions here. If something does not match your usecase, you have to adapt to it. In the end you need to have a clear picture in your mind how the end result should look like. We can’t answer that for you.