-2

How can I efficiently merge multiple orderedDictionarys?

In C#, I have checked online, but I could only see implementations for merging NamevaluePairs.

I am trying to join the below orderedDictionaries:

OrderedDictionary ticketToPickerMapForVerifiedTab = new OrderedDictionary();
OrderedDictionary ticketToPickerMapForHVTab = new OrderedDictionary();
OrderedDictionary ticketToPickerMapForKPTab = new OrderedDictionary();

The keys in those dictionaries are unique

I need the resultant collection to still be an OrderedDictionary object.

Please note that my question is different from the one asked here: Merging dictionaries in C#

As that one demonstrated a case for Keyvaluepair<TKey,TValue>, while mine refers to using OrderedDictionary, because of the special functionality of OrderedDictionary consumed elsewhere in my code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Edwin O.
  • 4,998
  • 41
  • 44
  • because without just call ticketToPickerMapForVerifiedTab.ToList(); AddRange(ticketToPickerMapForHVTab.ToList()) And the call toDictionary again – Alex Krupka Nov 08 '15 at 19:57
  • @AlexKrupka i do not understand your suggestion, could you be more elaborate there – Edwin O. Nov 08 '15 at 20:01
  • Possible duplicate of [Merging dictionaries in C#](http://stackoverflow.com/questions/294138/merging-dictionaries-in-c-sharp) – Bushuev Nov 08 '15 at 20:55
  • @Edwin try to change your code to use `SortedDictionary` instead of `OrderedDictionary`, then you can use many Linq tricks easily... – Eser Nov 08 '15 at 21:37
  • @Eser , sorry i can't change that, i am using the index functionality of orderedDictionary – Edwin O. Nov 08 '15 at 22:53

2 Answers2

1

You can use LINQ to merge them. See the code below:

var dictionariesMerged = ticketToPickerMapForVerifiedTab.Cast<DictionaryEntry>()
    .Union(ticketToPickerMapForHVTab.Cast<DictionaryEntry>())
    .Union(ticketToPickerMapForKPTab.Cast<DictionaryEntry>());

var dictionary = new OrderedDictionary();

foreach (DictionaryEntry tuple in dictionariesMerged)
    dictionary.Add(tuple.Key, tuple.Value);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alberto Monteiro
  • 5,989
  • 2
  • 28
  • 40
1

Something like the below 'MergeDictionaries' method will merge the contents of the given dictionaries. This does not handle the case where one key exists in multiple dictionaries.

[TestFixture]
public class Class1
{

    private OrderedDictionary MergeDictionaries(params OrderedDictionary[] dictionaries)
    {
        var merged = new OrderedDictionary();
        foreach (var dictionary in dictionaries)
        {
            foreach (DictionaryEntry entry in dictionary)
            {
                merged.Add(entry.Key, entry.Value);
            }
        }
        return merged;
    }

    [Test]
    public void Test()
    {
        var d1 = new OrderedDictionary
        {
            {"x", "v1"},
            {"b", "v2"},
        };
        var d2 = new OrderedDictionary
        {
            {"d", "v3" },
            {"c", "v4" }
        };
        var d3 = new OrderedDictionary
        {
            {"z", "v5" },
            {"y", "v6" }
        };

        var merged = MergeDictionaries(d1, d2, d3);

        var expected = new OrderedDictionary
        {
            {"b", "v2"},
            {"c", "v4" },
            {"d", "v3" },
            {"x", "v1"},
            {"y", "v6" },
            {"z", "v5" }
        };

        Assert.That(merged, Is.EquivalentTo(expected));
    }
}
lenkan
  • 4,089
  • 1
  • 14
  • 12