score:2

Accepted answer

this is what you are looking to do:

  • merge the two collections of dictionaries.
  • group items by the "id" key-value.
  • for each group you have multiple dictionaries so use selectmany to flatten and then groupby on the key. now you can recreate the dictionaries - todictionary. notice that you might have keys repeating themselves so that is why the nested groupby and for the value select the one you want. here i just used firstordefault

so:

var result = firstsourcedata.concat(secondsourcedata)
                .groupby(item => item["id"])
                .select(group => group.selectmany(item => item)
                                      .groupby(item => item.key)
                                      .todictionary(key => key.key, 
                                                    value => value.firstordefault().value));

this is the result:

new dictionary<string, object>
{
    ["id"] = 1,
    ["sales"] = 58,
    ["name"] = "some",
    ["age"] = 30
},
new dictionary<string, object>
{
    ["id"] = 2,
    ["sales"] = 58,
    ["age"] = 30
}

for this test case:

var firstsourcedata = new list<dictionary<string, object>>
{
    new dictionary<string, object>
    {
        ["id"] = 1,
        ["sales"] = 58,
        ["age"] = 30
    },
    new dictionary<string, object>
    {
        ["id"] = 2,
        ["sales"] = 58,
        ["age"] = 30
    }
};

var secondsourcedata = new list<dictionary<string, object>>
{
    new dictionary<string, object>
    {
        ["id"] = 1,
        ["name"] = "some",
        ["age"] = 30
    }
};

score:-1

you can do union:

dictionary<string,object> dict1 = new dictionary<string, object>();
        dict1.add("id", 1);
        dict1.add("name", "some");
        dict1.add("age", 30);

        dictionary<string, object> dict2 = new dictionary<string, object>();
        dict2.add("id", 1);
        dict2.add("sales", 58);
        dict2.add("age", 30);

        dictionary<string, object> dict3 = new dictionary<string, object>();
        dict3 = dict1.union(dict2).todictionary(c => c.key, c => c.value);

resulting values are:

[0] = {[id, 1]}
[1] = {[name, some]}
[2] = {[age, 30]}
[3] = {[sales, 58]}

score:0

you can use join like this

from dict1 in firstsourcedata
join dict2 in secondsourcedata
on dict1["id"] equals dict2["id"]
select dict1.concat(                                      //concatenates 2 dictionaries together
          dict2.where(kv => !dict1.containskey(kv.key))   //chooses non-repeating keys
       ).todictionary(kv => kv.key, kv => kv.value)       //gets a new dictionary from that

if the lengths of the sources are different join will only select these dictionaries, whose ids are in the first source. if you want to get all ids, regardless whether you have data from both sources, you can use defaultifempty

from dict1 in firstsourcedata
join tempdict2 in secondsourcedata
on dict1["id"] equals tempdict2["id"] into joined
from dict2 in joined.defaultifempty(new dictionary<string, object>()) //make a new dictionary if there's none
select dict1.concat(
          dict2.where(kv => !dict1.containskey(kv.key))
       ).todictionary(kv => kv.key, kv => kv.value)

if you want dictionaries from both regardless of ids refer to this question.


Related Query

More Query from same tag