4

Knowing this question's answer, what is the way to join 2 (only) dictionaries (.Net 4.5)?

Duplicates not admitted, the first one wins (see example bellow).

using System;
using System.Linq;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var d1 = new Dictionary<int, string>();
        var d2 = new Dictionary<int, string>();
        // combine both, keep first duplicate only 
        var result = d1.XXX?(d2); 

    }
}

say

[<1,'a'>, <2, 'b'>] + [<3,'c'>, <1, 'x'>] = > [<1,'a'>, <2, 'b'>, <3,'c'>]

serge
  • 13,940
  • 35
  • 121
  • 205
  • If those were two Database tables with similar key values, I would use a join. I guess linq has a join equivalent too? – Christopher Nov 23 '18 at 17:16
  • You already linked to an answer that seems to meet your needs. What about that answer does not work for you? – D Stanley Nov 23 '18 at 17:17
  • @DStanley there are a collection of dictionaries. But I have only two. should I create a collection only for that? – serge Nov 23 '18 at 17:18
  • Why havent you at least provided a meaningful example? – Tim Schmelter Nov 23 '18 at 17:18
  • @Serge you could - just use `var dicts = new [] {d1, d2}`. A join might work too, but they are much more complicated in method syntax. There's not a simple built-in function that will do it. – D Stanley Nov 23 '18 at 17:19
  • `[<1,'a'>, <2, 'b'>] + [<3,'c'>, <1, 'x'>] = > [<1,'a'>, <2, 'b'>, <3,'c'>]` – serge Nov 23 '18 at 17:20
  • @DStanley, I obtain an array, not a dictionary in your suggestion – serge Nov 23 '18 at 17:21
  • Possible duplicate of [Merging dictionaries in C#](https://stackoverflow.com/questions/294138/merging-dictionaries-in-c-sharp) – johnny 5 Nov 23 '18 at 17:39
  • even if specifying the version of .Net, I don't see how this question is any different from the one you linked to – johnny 5 Nov 23 '18 at 17:40
  • @johnny5, please read carefully my comments above – serge Nov 23 '18 at 19:14
  • I read your comments, but I would argue that the complexity between the two are trivial. In both causes you will be operating on an IEnumerable before merging into one, the only difference is that this provides more limitations than the prior answers – johnny 5 Nov 23 '18 at 19:53
  • @johnny5, when two, I have any IEnumerable anywhere. this is why my question. Incdrebible how you insist to make a duplicate from a question I strarted with, so didn't find an answer there. – serge Nov 23 '18 at 20:46
  • @Serge my point is that there is no benefit to creating this limitation. The answers from the original question will merge the dictionaries. So using a simple wrapper to call the original answer will work. I don’t think you’re confused on how to build a wrapper to call the original answer, if that’s the case the question should be rephrased. – johnny 5 Nov 23 '18 at 21:01
  • I don't understand what kind of wrapper I should create, once again I have no *one* array of *two* elements, I have just **two separate** elements. there is difference when you operate one single array, vs when you operate two entities..., say c = a+b is one thing, but the approach is different when you have `int []` and like to build a summ of elements. usually the second is a little bit more complex, the fist approach is easier, but *totally different*. – serge Nov 23 '18 at 21:21
  • compare `c = a + b` vs `c = myArr.Sum()` – serge Nov 23 '18 at 21:25

2 Answers2

10

You merge them like this:

var d1 = new Dictionary<int, string>() { [1] = "one" };
var d2 = new Dictionary<int, string>() { [1] = "un", [2] = "deux" };

var merged = d1.Concat(d2)
    .ToLookup(x => x.Key, x => x.Value)
    .ToDictionary(x => x.Key, g => g.First());
Xiaoy312
  • 14,292
  • 1
  • 32
  • 44
  • thanks, remark the .NET version ) But I see the idea! ;) – serge Nov 23 '18 at 17:24
  • Should be available: https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolookup?view=netframework-4.5 – Xiaoy312 Nov 23 '18 at 17:43
  • 1
    I mean your dictionary initialization, that appeared with C# 6 ;) – serge Nov 23 '18 at 21:29
  • 1
    Oh. Indeed, it was not supported. – Xiaoy312 Nov 23 '18 at 21:37
  • 3
    Although `ToLookup` is fine it's little bit more expensive than `GroupBy` because it creates another collection in memory before creating the final `Dictionary`. I'd use it only if the lookup is my desired collection. – Tim Schmelter Nov 26 '18 at 09:47
10

You can use Concat, GroupBy and First:

var result = d1.Concat(d2)
               .GroupBy(kv => kv.Key)
               .ToDictionary(g => g.Key, g => g.First().Value);
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • duplicates are not possible, as I say to keep fist value only (see the OP example), thank you – serge Nov 23 '18 at 17:23
  • I see, but you started the answer with a supposition that is not admitted in the OP. ) thanks – serge Nov 23 '18 at 17:25
  • the only difference from the other question is that I have exactly two, and not an array of dictionaries. I just wouldn't create an array of two dictionaries it seems unatural to me... – serge Nov 23 '18 at 17:30