开发者

Merging dictionaries in key level and then in value level

I have two dictionar开发者_高级运维ies like

Dictionary<String, String> one = new Dictionary<string, string>
{
    { "A", "1" },
    { "B", "2" },
    { "C", "3" },
    { "D", "4" },
    { "E", "5" },
    { "F", "6" },
    { "G", "7" },
    { "H", "8" }
};

Dictionary<String, String> two = new Dictionary<string, string>
{
    { "A", "1" },
    { "B", "2" },
    { "C", "3" },
    { "E", "4" },
    { "F", "4" },
    { "I", "6" },
    { "J", "10" },
    { "K", "11" }
};

i need to merge the two dictionary in key level and then in value level and to add the resulting dictionary into the new dictionary three, the resulting dictionary should not have same keys or same values and in this case the resulting dictionary is like

Dictionary<String, String> three = new Dictionary<string, string>
{
    { "A", "1" },
    { "B", "2" },
    { "C", "3" },
    { "D", "4" },
    { "E", "5" },
    { "F", "6" },
    { "G", "7" },
    { "H", "8" },
    { "J", "10" },
    { "K", "11" }
};

Now i'm using like

  1. Union all the keys in the two dictionaries
  2. Creating a new dictionary with the new keys
  3. removing the dupicate values( same values )

EDIT: if both the dictionaries having same key value pair then i need to store the the key value pair from the first dictionary .

is there any way to do this using LINQ? Thanks in advance


One option, using the fact that a dictionary is a sequence of key/value pairs:

var dictionary = dictionary1.Concat(dictionary2)
                            .ToLookup(pair => pair.Key, pair => pair.Value)
                            .ToDictionary(x => x.Key, x => x.First());

That's not terribly efficient, admittedly (as it essentially builds up a hash table twice) but I believe it will work.


A linq-only method would be to concatenate the two dictionaries and fold the resulting sequence of key/values into a single result. This will overwrite the values for any keys in dict2 that are also in dict1:

var dict3 = dict2.Concat(dict1)
    .Aggregate(new Dictionary<string, string>(), (d, kvp) => {
        d[kvp.Key] = kvp.Value;
        return d;
    });


class StringKeyValuePairEqualityComparer : IEqualityComparer<KeyValuePair<string, string>>
{
    public bool Equals(KeyValuePair<string, string> x, KeyValuePair<string, string> y)
    {
        return x.Key == y.Key;
    }

    public int GetHashCode(KeyValuePair<string, string> obj)
    {
        return obj.Key.GetHashCode();
    }
}

var three = Enumerable.Concat(one, two)
                .Distinct(new StringKeyValuePairEqualityComparer())
                .ToDictionary(p => p.Key, p => p.Value);

int count = three.Keys.Count; // 11


var three = new Dictionary<string, string>();
foreach(var kvp in two.Concat(one))
  three[kvp.Key] = kvp.Value;

This is quite efficient, but I'm not sure if this is the output you want; the problem statement is not clear enough.

EDIT: If you want to subsequently remove duplicate values from three:

var keysWithDuplicateValues = three.ToLookup(kvp => kvp.Value, kvp => kvp.Key)
                                   .SelectMany(group => group.Skip(1))
                                   .ToList();

foreach(var key in keysWithDuplicateValues)
   three.Remove(key);   

Note that this is better than eagerly removing duplicate keys and duplicate values at the start, because some of the collisions may be automatically resolved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜