Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 530129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:09:52+00:00 2026-05-13T09:09:52+00:00

I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two

  • 0

I have a Dictionary of dictionaries :

SortedDictionary<int,SortedDictionary<string,List<string>>>

I would like to merge two dictionaries for example key = 2 and key = 3.

Very important I could have duplicate keys in the respective dictionaries.

example key = 2 has a dictionary Key,Value "1000",{a,b,c}
and key = 3 has a dictionary key,Value "1000",{z}.

So I would like to merge key 2 and key 3 and the result would be the following Sorted Dictionary key, Value "1000",{a,b,c,z}.

I am a beginner with LINQ syntax so could you help solve this with detail code..

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:09:53+00:00Added an answer on May 13, 2026 at 9:09 am

    This is not a LINQ problem. Here’s a generic version that will solve your problem.

    static class SortedDictionaryExtensions {
        public static void MergeKeys<TKey1, TKey2, TValue>(
            this SortedDictionary<TKey1, SortedDictionary<TKey2, List<TValue>>> dictionary,
            TKey1 intoKey,
            TKey1 fromKey
        ) {
            if (dictionary == null) {
                throw new ArgumentNullException("dictionary");
            }
            if (intoKey == null) {
                throw new ArgumentNullException("intoKey");
            }
            if (fromKey == null) {
                throw new ArgumentNullException("fromKey");
            }
    
            SortedDictionary<TKey2, List<TValue>> to;
            SortedDictionary<TKey2, List<TValue>> from;
            if (!dictionary.TryGetValue(intoKey, out to)) {
                throw new ArgumentOutOfRangeException("intoKey");
            }
            if (!dictionary.TryGetValue(fromKey, out from)) {
                throw new ArgumentOutOfRangeException("fromKey");
            }
            foreach(TKey2 key in from.Keys) {
                if (to.Keys.Contains(key)) {
                    to[key].AddRange(from[key]);
                }
                else {
                    to.Add(key, from[key]);
                }
            }
            dictionary.Remove(fromKey);
        }
    }
    

    Usage:

     SortedDictionary<int, SortedDictionary<string, List<string>>> list = 
         new SortedDictionary<int, SortedDictionary<string, List<string>>>();
     list.Add(2, new SortedDictionary<string, List<string>>());
     list[2].Add("1000", new List<string>() { "a", "b", "c" });
     list[2].Add("2000", new List<string>() { "b", "c" });
     list.Add(4, new SortedDictionary<string, List<string>>());
     list[4].Add("1000", new List<string>() { "z" });
     list[4].Add("3000", new List<string>() { "y" });
    
     list.MergeKeys(2, 4);
    

    Here’s how you approach a problem like this. First, specify what you’re trying to do.

    Given a SortedDictionary<TKey1, SortedDictionary<TKey2, List<TValue>>> and two keys intoKey and fromKey in the dictionary, merge the dictionary with key fromKey into the dictionary with key intoKey.

    Now specify what it means to merge two dictionaries. Given two dictionaries to and from of type SortedDictionary<TKey2, List<TValue>> to merge them means the following. For each TKey2 key in from there are two possiblities:

    1. key is in to. In this case add the list from[key] to the list to[key].
    2. key is not in to. In this case add key to to with value from[key].

    Then, remove key fromKey from the dictionary.

    Let’s translate this to code:

    Given a SortedDictionary<TKey1, SortedDictionary<TKey2, List<TValue>>> and two keys intoKey and fromKey in the dictionary

    SortedDictionary<TKey2, List<TValue>> to;
    SortedDictionary<TKey2, List<TValue>> from;
    // check that dictionary has intoKey
    if (!dictionary.TryGetValue(intoKey, out to)) {
        throw new ArgumentOutOfRangeException("intoKey");
    }
    // check that dictionary has fromKey
    if (!dictionary.TryGetValue(fromKey, out from)) { 
         throw new ArgumentOutOfRangeException("fromKey");
    }
    

    For each TKey2 key in from there are two possiblities:

    foreach(TKey2 key in from.Keys) {
         // key is in to
         if (to.Keys.Contains(key)) {
              // add the list from[key] to the list to[key]
              to[key].AddRange(from[key]); 
         }
         // key is not in to
         else { 
              // add an entry (key, from[key]) to the dictionary
              to.Add(key, from[key]); 
         }
     }
    

    Then, remove key fromKey from the dictionary.

    dictionary.Remove(fromKey);
    

    The rest of the code is just error checking

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Dictionary<string,int> that has the potential to contain upwards of 10+ million
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
I have an array filled with dictionaries. Each dictionary have strings like title, info
I have 4 dictionaries of type Dictionary<string,string> dict1 k1 = v1 k2 = v2
I have a Dictionary<string, someobject> . EDIT: It was pointed out to me, that
I have a dictionary that I normally access with a key, so I need
I have one array that is pre-filled with dictionaries with each dictionary consisting of
basically I have an array of dictionaries and each dictionary has a day ,
I have a dictionary of 200,000 items (the keys are strings and the values
I have a Dictionary where I hold data for movieclips, and I want the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.