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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:44:41+00:00 2026-06-10T03:44:41+00:00

I have multiple dynamic objects that (most of the time) differ in only a

  • 0

I have multiple dynamic objects that (most of the time) differ in only a few values. I want to be able to merge these objects into one single object and in case of a conflict (two values aren’t the same) then I want these to be stored in a collection or another dynamic object (if possible).

I’m usisng the expandoObject class so I can cast my objects into a dictionairy and try to merge that but I haven’t found any articles or sources on merging dictionairies that create collections with conflicts.

Is there a way to do this in an easy to implement and efficient fashion?

I’ll post some code samples to give you a small idea of what I’m trying to accomplish

//returns a dynamic object from JSON (output from DataBase)
JsonReader reader = new JsonReader();
dynamic object = reader.Read(input);

//Creates a dictionairy with all the fieldnames and values.
IDictionary<string, dynamic> properties = (IDictionary<string, dynamic>)object;

//My goal is to merge multiple of these objects into one single object and
//creating collections when values are different from each other.
  • 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-06-10T03:44:43+00:00Added an answer on June 10, 2026 at 3:44 am

    Your problem seems to be based on the IDictionary<> interface. It assumes there is only one value for each key. I believe that you want rather to move to a LINQ-ish IEnumerable<IGrouping<TKey,TValue>> that represents a list of keyed value collections.

    LINQ emits such objects when you do a .GroupBy or .ToLookup calls.
    Let’s play then:

    using System.Linq;
    
    Dictionary<string, dynamic> A = ...;
    Dictionary<string, dynamic> B = ...;
    
    // naiive atempt:
    var lookup = 
        A.Keys.Concat(B.Keys)
            .ToDictionary(key => key, key => new dynamic[]{ A[key], B[key] } ));
    

    Of course it will not work, but I’ve written it to see what problems would it expose. First – you will probably get duplicate keys when Concat’ing. Then not all keys are in A and B, so the indexers woudl throw. Next, I happened to assume that the original objects were string-to-one-value, and you are likely to work on already-collided objectts. Then, this uses only 2 A/B objects, while you may want to work on multiple..

    IEnumerable<Dictionary<string, dynamic>> inputs = ....;
    
    // btw. GropuBy returns a lookup, too :) a key -> all matches
    var matched_by_keys = inputs.GroupBy(pair => pair.Key);
    
    var final = matched_by_keys.ToDictionary(group => group.Key, group => group);
    

    Look there. I’ve taken all dictionaries and “just paired them up” according to their Keys. As a result, I’ve got a lookup that binds each existing Key to a series of Values that were earlier assigned to that key. The resulting matched_by_keys is an Enumerable, not a dictionary, so it was later translated to it. Look at the parameters to ToDictionary: the Group is itself a IEnumerable just a key had to be pointed out and the group is unchanged.

    Still, the input works only on IDictionary that is single-valued. If you need to do such things with multi-valued inputs too, you can easily transalte IDictionaries to lookups and perform the operations on them:

    IEnumerable<Dictionary<string, dynamic>> inputs1 = ....; // normal items
    IEnumerable<ILookup<string, dynamic>> inputs2 = ....; // items that previously already collided
    
    var allAsLookups =
        inputs1.ToLookup(pair => pair.Key, pair => pair.Value)
        .Concat( inputs2 );
    
    // btw. GropuBy returns a lookup, too :) a key -> all matches
    var matched_by_keys = lookups.GroupBy(lk => lk.Key);
    
    var final1 = matched_by_keys.ToDictionary(group => group.Key, group => group.SelectMany(s=>s));
    

    Note how the final groups had to be now translated. In this example, groupsis not IEnumerable<Value>, but IEnumerable<IEnumerable<Value>> becuase the inputs were allowed to be multi-valued, even if they had only 1 value. So, thit had to be flattened, and this is done by SelectMany. That in turn didnt need to project anything, as the item was already IEnumerable, so a s=>s was enough.

    Using different overloads of GroupBy, ToLookup, and ToDictionary you may achieve many useful effects. Play with the overloads!

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

Sidebar

Related Questions

I have a class that contains multiple user objects and as such has an
I have a very dynamic web app that is dynamically created controls at run-time.
I'm making an App that will have multiple, identical, objects and I need to
Using storyboard, I have a table view controller containing multiple dynamic prototype cells. One
I have a dynamic search query with join multiple tables, for showing sum and
I have multiple forms for lots of products on my page and want to
I have multiple calls to methods in a 3rd party library. These methods have
I have a class ( MyClass ) that inherits most of its functionality from
I have a stored procedure that returns a dynamic query, e,g if i pass
I have a view with multiple forms on it. These forms have partial views

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.