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 3633196
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:37:21+00:00 2026-05-19T00:37:21+00:00

I have two dictionaries in c#. The Two Dictionaries and their calues are Dictionary<int,List<string>>

  • 0

I have two dictionaries in c#.

The Two Dictionaries and their calues are

 Dictionary<int,List<string>> D1 = new Dictionary<int,List<string>>(); 
 Dictionary<int,List<string>> D2= new Dictionary<int,List<string>>(); 
 Dictionary<int,List<string>> D3 new Dictionary<int,List<string>>();
 D1[1] = new List<string>{"a","b"};
 D1[2] = new List<string>{"c","d"}; 
 D1[3] = new List<string>{"e","f"}; 
 D1[4] = new List<string>{"h"};  

Where 1,2,3 and 4 are keys of Dictionary D1

 D2[1] = new List<string>{"a","b"}; 
 D2[2] = new List<string>{"c","d"}; 
 D2[3] = new List<string>{"e","f"};
 D2[4] = new List<string>{"g"}; 
 D2[5] = new List<string>{"b","h"};
 D2[6] = new List<string>{"f","l"};
 D2[7] = new List<string>{"z"}; 

Where 1,2,3,4,5,6 and 7 are keys of Dictionary D2

Then the output Dictionary Contains this values,

 D3[1] = {"a","b","h"}   D3[2] = {"c","d"}   D3[3] = {"e","f","l"} 

Note: Please take the Input Dictionary with values greater than 1.Thats why i am eliminating the D1[4] , D2[4] and D2[7]

IS IT POSSIBLE TO MERGE IT USING LINQ?

  • 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-19T00:37:21+00:00Added an answer on May 19, 2026 at 12:37 am

    Yes it’s possible but it’s not pretty!

    //firstly lets get the keys that are valid (i.e. have more than one element in their list)
    var validD1Elements = D1.Where(d => d.Value.Count > 1);
    var validD2Elements = D2.Where(d => d.Value.Count > 1);
    
    //merge the valid keys together so we know which ones we want to select
    var mergedKeys = validD1Elements.Select(d => d.Key).Union(validD2Elements.Select(d => d.Key));
    
    //perform the merge
    var mergeResult = mergedKeys.Select (key => new
    {
        Key = key,
        //select the values from D1
        Value = validD1Elements.Where(d => d.Key == key).SelectMany(d => d.Value)
        //concat the values from D2
        .Concat(validD2Elements.Where(d => d.Key == key).SelectMany(d => d.Value))
    }).ToDictionary(e => e.Key, e => e.Value);
    

    This merge uses Concat so you will get duplicates, i.e. mergeResult[1] will be { "a", "b", "a", "b" }.

    If you do not want duplicates change the following code from this:

    //concat the values from D2
    .Concat(validD2Elements.Where(d => d.Key == key).SelectMany(d => d.Value))
    

    to this:

    //union the values from D2
    .Union(validD2Elements.Where(d => d.Key == key).SelectMany(d => d.Value))
    

    mergeResult[1] will then be { "a", "b" }.

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

Sidebar

Related Questions

I have two dictionaries with the same structure: Dictionary<string, int> foo = new Dictionary<string,
I have two complex dictionaries in the form Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> So as
Here is my problem: I have two dictionaries with identical structures: Dictionary<string, List<Object>> Existing
I have two dictionaries like Dictionary<String, String> one = new Dictionary<string, string> { {
So I have two dictionaries. Dictionary 1 looks like this: {'aaien': ['01, 02, 12'],
I have two generic Dictionaries. Both have the same keys, but their values can
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
There are two dictionaries like this, two dictionaries have same keys but different values.
I have two dictionaries containing a string key and then an object. The object
I have two dictionaries in Python. One is a list of teams and their

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.