Merge two dictionaries in C#





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-2















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'>]










share|improve this question

























  • 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?

    – Rango
    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




















-2















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'>]










share|improve this question

























  • 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?

    – Rango
    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
















-2












-2








-2








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'>]










share|improve this question
















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'>]







c# .net-4.5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 17:28







Serge

















asked Nov 23 '18 at 17:12









SergeSerge

3,29734293




3,29734293













  • 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?

    – Rango
    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





















  • 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?

    – Rango
    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



















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







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







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





@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?

– Rango
Nov 23 '18 at 17:18





Why havent you at least provided a meaningful example?

– Rango
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







@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














2 Answers
2






active

oldest

votes


















1














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());





share|improve this answer
























  • thanks, remark the .NET version ) But I see the idea! ;)

    – Serge
    Nov 23 '18 at 17:24











  • Should be available: docs.microsoft.com/en-us/dotnet/api/…

    – 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











  • 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.

    – Rango
    Nov 26 '18 at 9:47





















1














You can use Concat, GroupBy and First:



var result = d1.Concat(d2)
.GroupBy(kv => kv.Key)
.ToDictionary(g => g.Key, g => g.First().Value);





share|improve this answer


























  • 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











  • already did, thank you. )

    – Serge
    Nov 23 '18 at 17:33












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450672%2fmerge-two-dictionaries-in-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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());





share|improve this answer
























  • thanks, remark the .NET version ) But I see the idea! ;)

    – Serge
    Nov 23 '18 at 17:24











  • Should be available: docs.microsoft.com/en-us/dotnet/api/…

    – 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











  • 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.

    – Rango
    Nov 26 '18 at 9:47


















1














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());





share|improve this answer
























  • thanks, remark the .NET version ) But I see the idea! ;)

    – Serge
    Nov 23 '18 at 17:24











  • Should be available: docs.microsoft.com/en-us/dotnet/api/…

    – 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











  • 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.

    – Rango
    Nov 26 '18 at 9:47
















1












1








1







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());





share|improve this answer













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());






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 17:21









Xiaoy312Xiaoy312

11.6k12234




11.6k12234













  • thanks, remark the .NET version ) But I see the idea! ;)

    – Serge
    Nov 23 '18 at 17:24











  • Should be available: docs.microsoft.com/en-us/dotnet/api/…

    – 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











  • 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.

    – Rango
    Nov 26 '18 at 9:47





















  • thanks, remark the .NET version ) But I see the idea! ;)

    – Serge
    Nov 23 '18 at 17:24











  • Should be available: docs.microsoft.com/en-us/dotnet/api/…

    – 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











  • 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.

    – Rango
    Nov 26 '18 at 9:47



















thanks, remark the .NET version ) But I see the idea! ;)

– Serge
Nov 23 '18 at 17:24





thanks, remark the .NET version ) But I see the idea! ;)

– Serge
Nov 23 '18 at 17:24













Should be available: docs.microsoft.com/en-us/dotnet/api/…

– Xiaoy312
Nov 23 '18 at 17:43





Should be available: docs.microsoft.com/en-us/dotnet/api/…

– Xiaoy312
Nov 23 '18 at 17:43




1




1





I mean your dictionary initialization, that appeared with C# 6 ;)

– Serge
Nov 23 '18 at 21:29





I mean your dictionary initialization, that appeared with C# 6 ;)

– Serge
Nov 23 '18 at 21:29




1




1





Oh. Indeed, it was not supported.

– Xiaoy312
Nov 23 '18 at 21:37





Oh. Indeed, it was not supported.

– Xiaoy312
Nov 23 '18 at 21:37













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.

– Rango
Nov 26 '18 at 9:47







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.

– Rango
Nov 26 '18 at 9:47















1














You can use Concat, GroupBy and First:



var result = d1.Concat(d2)
.GroupBy(kv => kv.Key)
.ToDictionary(g => g.Key, g => g.First().Value);





share|improve this answer


























  • 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











  • already did, thank you. )

    – Serge
    Nov 23 '18 at 17:33
















1














You can use Concat, GroupBy and First:



var result = d1.Concat(d2)
.GroupBy(kv => kv.Key)
.ToDictionary(g => g.Key, g => g.First().Value);





share|improve this answer


























  • 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











  • already did, thank you. )

    – Serge
    Nov 23 '18 at 17:33














1












1








1







You can use Concat, GroupBy and First:



var result = d1.Concat(d2)
.GroupBy(kv => kv.Key)
.ToDictionary(g => g.Key, g => g.First().Value);





share|improve this answer















You can use Concat, GroupBy and First:



var result = d1.Concat(d2)
.GroupBy(kv => kv.Key)
.ToDictionary(g => g.Key, g => g.First().Value);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 17:31

























answered Nov 23 '18 at 17:21









RangoRango

367k46475739




367k46475739













  • 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











  • already did, thank you. )

    – Serge
    Nov 23 '18 at 17:33



















  • 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











  • already did, thank you. )

    – Serge
    Nov 23 '18 at 17:33

















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





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





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





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













already did, thank you. )

– Serge
Nov 23 '18 at 17:33





already did, thank you. )

– Serge
Nov 23 '18 at 17:33


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450672%2fmerge-two-dictionaries-in-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini