Merging 2 dictionaries having duplicate keys with linq
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
How to merge 2 dictionaries of IDictionary<Guid, MyObject>
where MyObject
is a class instance?
IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 = d1.Union(d2) ???
That in d3
there are the following entries:
guid1,m1
guid2,m2
guid3,m3
guid4,m4
c# linq dictionary merge
add a comment |
How to merge 2 dictionaries of IDictionary<Guid, MyObject>
where MyObject
is a class instance?
IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 = d1.Union(d2) ???
That in d3
there are the following entries:
guid1,m1
guid2,m2
guid3,m3
guid4,m4
c# linq dictionary merge
duplicate of stackoverflow.com/questions/294138/merging-dictionaries-in-c
– hatchet
Aug 1 '11 at 19:22
sorry, I could not find my solutiion there
– Chesnokov Yuriy
Aug 1 '11 at 19:43
possible duplicate of Combine two Dictionaries with linq; more exact duplicate than the other one
– Mechanical snail
Nov 21 '12 at 6:31
add a comment |
How to merge 2 dictionaries of IDictionary<Guid, MyObject>
where MyObject
is a class instance?
IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 = d1.Union(d2) ???
That in d3
there are the following entries:
guid1,m1
guid2,m2
guid3,m3
guid4,m4
c# linq dictionary merge
How to merge 2 dictionaries of IDictionary<Guid, MyObject>
where MyObject
is a class instance?
IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 = d1.Union(d2) ???
That in d3
there are the following entries:
guid1,m1
guid2,m2
guid3,m3
guid4,m4
c# linq dictionary merge
c# linq dictionary merge
edited Nov 21 '12 at 6:30
Mechanical snail
19.9k1074100
19.9k1074100
asked Aug 1 '11 at 19:13
Chesnokov YuriyChesnokov Yuriy
75531529
75531529
duplicate of stackoverflow.com/questions/294138/merging-dictionaries-in-c
– hatchet
Aug 1 '11 at 19:22
sorry, I could not find my solutiion there
– Chesnokov Yuriy
Aug 1 '11 at 19:43
possible duplicate of Combine two Dictionaries with linq; more exact duplicate than the other one
– Mechanical snail
Nov 21 '12 at 6:31
add a comment |
duplicate of stackoverflow.com/questions/294138/merging-dictionaries-in-c
– hatchet
Aug 1 '11 at 19:22
sorry, I could not find my solutiion there
– Chesnokov Yuriy
Aug 1 '11 at 19:43
possible duplicate of Combine two Dictionaries with linq; more exact duplicate than the other one
– Mechanical snail
Nov 21 '12 at 6:31
duplicate of stackoverflow.com/questions/294138/merging-dictionaries-in-c
– hatchet
Aug 1 '11 at 19:22
duplicate of stackoverflow.com/questions/294138/merging-dictionaries-in-c
– hatchet
Aug 1 '11 at 19:22
sorry, I could not find my solutiion there
– Chesnokov Yuriy
Aug 1 '11 at 19:43
sorry, I could not find my solutiion there
– Chesnokov Yuriy
Aug 1 '11 at 19:43
possible duplicate of Combine two Dictionaries with linq; more exact duplicate than the other one
– Mechanical snail
Nov 21 '12 at 6:31
possible duplicate of Combine two Dictionaries with linq; more exact duplicate than the other one
– Mechanical snail
Nov 21 '12 at 6:31
add a comment |
5 Answers
5
active
oldest
votes
d1.Concat(d2.Where( x=> !d1.Keys.Contains(x.Key)));
1
thank you very much, that is the correct approach though leading processing of the concatenated set
– Chesnokov Yuriy
Aug 1 '11 at 19:37
@Eric This looks like a good solution. But, if I haveIEnuemrable<Dictionary<Guid, MyObject>>
colection, how can I match items?
– barteloma
Aug 17 '17 at 13:10
add a comment |
d1.Union(d2).GroupBy (kvp => kvp.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.First().Value);
out to do the trick.
IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 =
d1.Union(d2).GroupBy (kvp => kvp.Key)
.ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value);
thanks but I tried that and Union leads to Exception with 'An item with the same key has already been added'
– Chesnokov Yuriy
Aug 1 '11 at 19:32
I fixed it. ` IDictionary<Guid, MyObject> d3 = d1.Union(d2).GroupBy (kvp => kvp.Key) .ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value); `
– agent-j
Aug 1 '11 at 22:20
thank you very much, that works, how that line avoids duplicate keys? is that faster thand1.Concat(d2.Where(x => !d1.Keys.Contains(x.Key)))
?
– Chesnokov Yuriy
Aug 2 '11 at 5:27
I tested that using Stopwatch, using of Concat is 3 time faster, SO DO NOT USE Union
– NET3
Nov 22 '12 at 23:00
1
Much more readable than accepted answer.
– nawfal
May 26 '16 at 12:40
add a comment |
You could try something like
d1.Concat(d2).Distinct(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value)
The result of concat makes use of the fact that the dictionary is an IEnumerable<KeyvaluePair<Guid,MyObject>>
Since I do not have a compiler I just checked that Distinct cannot accept just a lambda selecting the property to be compared. However it can accept an EqualityComparer. What I often have in projects is a Generic Equality Comparer that allows to pass in lambdas which define the equality operation.
yes, equality comparer is needed
– Chesnokov Yuriy
Aug 1 '11 at 19:39
add a comment |
When no duplicates keys exist, the following works for 2 (or more) dictionaries:
var dictionaries = new { d1, d2 };
var result = dictionaries.SelectMany(dict => dict)
.ToDictionary(pair => pair.Key, pair => pair.Value);
1
ok, there are duplicates present
– Chesnokov Yuriy
Aug 1 '11 at 19:40
add a comment |
Union looks good:
http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#union1
that does not work well with class instance in the value
– Chesnokov Yuriy
Aug 1 '11 at 19:35
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6903175%2fmerging-2-dictionaries-having-duplicate-keys-with-linq%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
d1.Concat(d2.Where( x=> !d1.Keys.Contains(x.Key)));
1
thank you very much, that is the correct approach though leading processing of the concatenated set
– Chesnokov Yuriy
Aug 1 '11 at 19:37
@Eric This looks like a good solution. But, if I haveIEnuemrable<Dictionary<Guid, MyObject>>
colection, how can I match items?
– barteloma
Aug 17 '17 at 13:10
add a comment |
d1.Concat(d2.Where( x=> !d1.Keys.Contains(x.Key)));
1
thank you very much, that is the correct approach though leading processing of the concatenated set
– Chesnokov Yuriy
Aug 1 '11 at 19:37
@Eric This looks like a good solution. But, if I haveIEnuemrable<Dictionary<Guid, MyObject>>
colection, how can I match items?
– barteloma
Aug 17 '17 at 13:10
add a comment |
d1.Concat(d2.Where( x=> !d1.Keys.Contains(x.Key)));
d1.Concat(d2.Where( x=> !d1.Keys.Contains(x.Key)));
edited Nov 16 '18 at 13:42
croxy
2,95072039
2,95072039
answered Aug 1 '11 at 19:20
Eric HEric H
1,2811814
1,2811814
1
thank you very much, that is the correct approach though leading processing of the concatenated set
– Chesnokov Yuriy
Aug 1 '11 at 19:37
@Eric This looks like a good solution. But, if I haveIEnuemrable<Dictionary<Guid, MyObject>>
colection, how can I match items?
– barteloma
Aug 17 '17 at 13:10
add a comment |
1
thank you very much, that is the correct approach though leading processing of the concatenated set
– Chesnokov Yuriy
Aug 1 '11 at 19:37
@Eric This looks like a good solution. But, if I haveIEnuemrable<Dictionary<Guid, MyObject>>
colection, how can I match items?
– barteloma
Aug 17 '17 at 13:10
1
1
thank you very much, that is the correct approach though leading processing of the concatenated set
– Chesnokov Yuriy
Aug 1 '11 at 19:37
thank you very much, that is the correct approach though leading processing of the concatenated set
– Chesnokov Yuriy
Aug 1 '11 at 19:37
@Eric This looks like a good solution. But, if I have
IEnuemrable<Dictionary<Guid, MyObject>>
colection, how can I match items?– barteloma
Aug 17 '17 at 13:10
@Eric This looks like a good solution. But, if I have
IEnuemrable<Dictionary<Guid, MyObject>>
colection, how can I match items?– barteloma
Aug 17 '17 at 13:10
add a comment |
d1.Union(d2).GroupBy (kvp => kvp.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.First().Value);
out to do the trick.
IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 =
d1.Union(d2).GroupBy (kvp => kvp.Key)
.ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value);
thanks but I tried that and Union leads to Exception with 'An item with the same key has already been added'
– Chesnokov Yuriy
Aug 1 '11 at 19:32
I fixed it. ` IDictionary<Guid, MyObject> d3 = d1.Union(d2).GroupBy (kvp => kvp.Key) .ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value); `
– agent-j
Aug 1 '11 at 22:20
thank you very much, that works, how that line avoids duplicate keys? is that faster thand1.Concat(d2.Where(x => !d1.Keys.Contains(x.Key)))
?
– Chesnokov Yuriy
Aug 2 '11 at 5:27
I tested that using Stopwatch, using of Concat is 3 time faster, SO DO NOT USE Union
– NET3
Nov 22 '12 at 23:00
1
Much more readable than accepted answer.
– nawfal
May 26 '16 at 12:40
add a comment |
d1.Union(d2).GroupBy (kvp => kvp.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.First().Value);
out to do the trick.
IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 =
d1.Union(d2).GroupBy (kvp => kvp.Key)
.ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value);
thanks but I tried that and Union leads to Exception with 'An item with the same key has already been added'
– Chesnokov Yuriy
Aug 1 '11 at 19:32
I fixed it. ` IDictionary<Guid, MyObject> d3 = d1.Union(d2).GroupBy (kvp => kvp.Key) .ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value); `
– agent-j
Aug 1 '11 at 22:20
thank you very much, that works, how that line avoids duplicate keys? is that faster thand1.Concat(d2.Where(x => !d1.Keys.Contains(x.Key)))
?
– Chesnokov Yuriy
Aug 2 '11 at 5:27
I tested that using Stopwatch, using of Concat is 3 time faster, SO DO NOT USE Union
– NET3
Nov 22 '12 at 23:00
1
Much more readable than accepted answer.
– nawfal
May 26 '16 at 12:40
add a comment |
d1.Union(d2).GroupBy (kvp => kvp.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.First().Value);
out to do the trick.
IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 =
d1.Union(d2).GroupBy (kvp => kvp.Key)
.ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value);
d1.Union(d2).GroupBy (kvp => kvp.Key).ToDictionary(kvp => kvp.Key, kvp => kvp.First().Value);
out to do the trick.
IDictionary<Guid, MyObject> d1 = new Dictionary<Guid, MyObject>();
d1.Add(guid1, m1);
d1.Add(guid2, m2);
d1.Add(guid3, m3);
IDictionary<Guid, MyObject> d2 = new Dictionary<Guid, MyObject>();
d2.Add(guid2, m2);
d2.Add(guid3, m3);
d2.Add(guid4, m4);
IDictionary<Guid, MyObject> d3 =
d1.Union(d2).GroupBy (kvp => kvp.Key)
.ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value);
edited Nov 23 '12 at 21:17
answered Aug 1 '11 at 19:20
agent-jagent-j
22.4k43973
22.4k43973
thanks but I tried that and Union leads to Exception with 'An item with the same key has already been added'
– Chesnokov Yuriy
Aug 1 '11 at 19:32
I fixed it. ` IDictionary<Guid, MyObject> d3 = d1.Union(d2).GroupBy (kvp => kvp.Key) .ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value); `
– agent-j
Aug 1 '11 at 22:20
thank you very much, that works, how that line avoids duplicate keys? is that faster thand1.Concat(d2.Where(x => !d1.Keys.Contains(x.Key)))
?
– Chesnokov Yuriy
Aug 2 '11 at 5:27
I tested that using Stopwatch, using of Concat is 3 time faster, SO DO NOT USE Union
– NET3
Nov 22 '12 at 23:00
1
Much more readable than accepted answer.
– nawfal
May 26 '16 at 12:40
add a comment |
thanks but I tried that and Union leads to Exception with 'An item with the same key has already been added'
– Chesnokov Yuriy
Aug 1 '11 at 19:32
I fixed it. ` IDictionary<Guid, MyObject> d3 = d1.Union(d2).GroupBy (kvp => kvp.Key) .ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value); `
– agent-j
Aug 1 '11 at 22:20
thank you very much, that works, how that line avoids duplicate keys? is that faster thand1.Concat(d2.Where(x => !d1.Keys.Contains(x.Key)))
?
– Chesnokov Yuriy
Aug 2 '11 at 5:27
I tested that using Stopwatch, using of Concat is 3 time faster, SO DO NOT USE Union
– NET3
Nov 22 '12 at 23:00
1
Much more readable than accepted answer.
– nawfal
May 26 '16 at 12:40
thanks but I tried that and Union leads to Exception with 'An item with the same key has already been added'
– Chesnokov Yuriy
Aug 1 '11 at 19:32
thanks but I tried that and Union leads to Exception with 'An item with the same key has already been added'
– Chesnokov Yuriy
Aug 1 '11 at 19:32
I fixed it. ` IDictionary<Guid, MyObject> d3 = d1.Union(d2).GroupBy (kvp => kvp.Key) .ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value); `
– agent-j
Aug 1 '11 at 22:20
I fixed it. ` IDictionary<Guid, MyObject> d3 = d1.Union(d2).GroupBy (kvp => kvp.Key) .ToDictionary (kvp => kvp.Key, kvp => kvp.First ().Value); `
– agent-j
Aug 1 '11 at 22:20
thank you very much, that works, how that line avoids duplicate keys? is that faster than
d1.Concat(d2.Where(x => !d1.Keys.Contains(x.Key)))
?– Chesnokov Yuriy
Aug 2 '11 at 5:27
thank you very much, that works, how that line avoids duplicate keys? is that faster than
d1.Concat(d2.Where(x => !d1.Keys.Contains(x.Key)))
?– Chesnokov Yuriy
Aug 2 '11 at 5:27
I tested that using Stopwatch, using of Concat is 3 time faster, SO DO NOT USE Union
– NET3
Nov 22 '12 at 23:00
I tested that using Stopwatch, using of Concat is 3 time faster, SO DO NOT USE Union
– NET3
Nov 22 '12 at 23:00
1
1
Much more readable than accepted answer.
– nawfal
May 26 '16 at 12:40
Much more readable than accepted answer.
– nawfal
May 26 '16 at 12:40
add a comment |
You could try something like
d1.Concat(d2).Distinct(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value)
The result of concat makes use of the fact that the dictionary is an IEnumerable<KeyvaluePair<Guid,MyObject>>
Since I do not have a compiler I just checked that Distinct cannot accept just a lambda selecting the property to be compared. However it can accept an EqualityComparer. What I often have in projects is a Generic Equality Comparer that allows to pass in lambdas which define the equality operation.
yes, equality comparer is needed
– Chesnokov Yuriy
Aug 1 '11 at 19:39
add a comment |
You could try something like
d1.Concat(d2).Distinct(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value)
The result of concat makes use of the fact that the dictionary is an IEnumerable<KeyvaluePair<Guid,MyObject>>
Since I do not have a compiler I just checked that Distinct cannot accept just a lambda selecting the property to be compared. However it can accept an EqualityComparer. What I often have in projects is a Generic Equality Comparer that allows to pass in lambdas which define the equality operation.
yes, equality comparer is needed
– Chesnokov Yuriy
Aug 1 '11 at 19:39
add a comment |
You could try something like
d1.Concat(d2).Distinct(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value)
The result of concat makes use of the fact that the dictionary is an IEnumerable<KeyvaluePair<Guid,MyObject>>
Since I do not have a compiler I just checked that Distinct cannot accept just a lambda selecting the property to be compared. However it can accept an EqualityComparer. What I often have in projects is a Generic Equality Comparer that allows to pass in lambdas which define the equality operation.
You could try something like
d1.Concat(d2).Distinct(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value)
The result of concat makes use of the fact that the dictionary is an IEnumerable<KeyvaluePair<Guid,MyObject>>
Since I do not have a compiler I just checked that Distinct cannot accept just a lambda selecting the property to be compared. However it can accept an EqualityComparer. What I often have in projects is a Generic Equality Comparer that allows to pass in lambdas which define the equality operation.
answered Aug 1 '11 at 19:18
flqflq
18.3k34469
18.3k34469
yes, equality comparer is needed
– Chesnokov Yuriy
Aug 1 '11 at 19:39
add a comment |
yes, equality comparer is needed
– Chesnokov Yuriy
Aug 1 '11 at 19:39
yes, equality comparer is needed
– Chesnokov Yuriy
Aug 1 '11 at 19:39
yes, equality comparer is needed
– Chesnokov Yuriy
Aug 1 '11 at 19:39
add a comment |
When no duplicates keys exist, the following works for 2 (or more) dictionaries:
var dictionaries = new { d1, d2 };
var result = dictionaries.SelectMany(dict => dict)
.ToDictionary(pair => pair.Key, pair => pair.Value);
1
ok, there are duplicates present
– Chesnokov Yuriy
Aug 1 '11 at 19:40
add a comment |
When no duplicates keys exist, the following works for 2 (or more) dictionaries:
var dictionaries = new { d1, d2 };
var result = dictionaries.SelectMany(dict => dict)
.ToDictionary(pair => pair.Key, pair => pair.Value);
1
ok, there are duplicates present
– Chesnokov Yuriy
Aug 1 '11 at 19:40
add a comment |
When no duplicates keys exist, the following works for 2 (or more) dictionaries:
var dictionaries = new { d1, d2 };
var result = dictionaries.SelectMany(dict => dict)
.ToDictionary(pair => pair.Key, pair => pair.Value);
When no duplicates keys exist, the following works for 2 (or more) dictionaries:
var dictionaries = new { d1, d2 };
var result = dictionaries.SelectMany(dict => dict)
.ToDictionary(pair => pair.Key, pair => pair.Value);
answered Aug 1 '11 at 19:19
Chaim ZonnenbergChaim Zonnenberg
1,549119
1,549119
1
ok, there are duplicates present
– Chesnokov Yuriy
Aug 1 '11 at 19:40
add a comment |
1
ok, there are duplicates present
– Chesnokov Yuriy
Aug 1 '11 at 19:40
1
1
ok, there are duplicates present
– Chesnokov Yuriy
Aug 1 '11 at 19:40
ok, there are duplicates present
– Chesnokov Yuriy
Aug 1 '11 at 19:40
add a comment |
Union looks good:
http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#union1
that does not work well with class instance in the value
– Chesnokov Yuriy
Aug 1 '11 at 19:35
add a comment |
Union looks good:
http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#union1
that does not work well with class instance in the value
– Chesnokov Yuriy
Aug 1 '11 at 19:35
add a comment |
Union looks good:
http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#union1
Union looks good:
http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#union1
answered Aug 1 '11 at 19:26
Paul NikonowiczPaul Nikonowicz
2,6431234
2,6431234
that does not work well with class instance in the value
– Chesnokov Yuriy
Aug 1 '11 at 19:35
add a comment |
that does not work well with class instance in the value
– Chesnokov Yuriy
Aug 1 '11 at 19:35
that does not work well with class instance in the value
– Chesnokov Yuriy
Aug 1 '11 at 19:35
that does not work well with class instance in the value
– Chesnokov Yuriy
Aug 1 '11 at 19:35
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6903175%2fmerging-2-dictionaries-having-duplicate-keys-with-linq%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
duplicate of stackoverflow.com/questions/294138/merging-dictionaries-in-c
– hatchet
Aug 1 '11 at 19:22
sorry, I could not find my solutiion there
– Chesnokov Yuriy
Aug 1 '11 at 19:43
possible duplicate of Combine two Dictionaries with linq; more exact duplicate than the other one
– Mechanical snail
Nov 21 '12 at 6:31