如何在没有循环的情况下组合两个词典?

时间:2021-02-20 19:33:14

I have two dictionaries of type <string,object> in C#. How can I copy all the contents of one Dictionary object to the other without applying a loop?

我在C#中有两个类型为 的字典。如何在不应用循环的情况下将一个Dictionary对象的所有内容复制到另一个? ,object>

4 个解决方案

#1


var d3 = d1.Concat(d2).ToDictionary(x => x.Key, x => x.Value);

#2


You can use Concat:

你可以使用Concat:

Dictionary<string, object> d1 = new Dictionary<string, object>();
d1.Add("a", new object());
d1.Add("b", new object());
Dictionary<string, object> d2 = new Dictionary<string, object>();
d2.Add("c", new object());
d2.Add("d", new object());

Dictionary<string, object> d3 = d1.Concat(d2).ToDictionary(e => e.Key, e => e.Value);

foreach (var item in d3)
{
    Console.WriteLine(item.Key);
}

#3


First up, it's not possible without looping. Whether that loop is done in a (extension) method is irrelevent, it still requires a loop.

首先,没有循环就不可能。是否在(扩展)方法中完成该循环是无关紧要的,它仍然需要循环。

I'm actually going to recommend doing it manually. All the other answers given require using two extention methods (Concat - ToDictionary and SelectMany - ToDictionary) and thus looping twice. If you are doing this to optimise your code, it will be faster to do a loop over dictionary B and add it's contents to dictionary A.

我实际上会建议手动完成它。给出的所有其他答案都需要使用两种扩展方法(Concat - ToDictionary和SelectMany - ToDictionary),因此循环两次。如果你这样做是为了优化你的代码,那么在字典B上做一个循环并将它的内容添加到字典A会更快。

Edit: After further investigation, the Concat operation would only occur during the ToDictionary call, but I still think a custom extension method would be more efficient.

编辑:进一步调查后,Concat操作只会在ToDictionary调用期间发生,但我仍然认为自定义扩展方法会更有效。

If you want to reduce your code size, then just make an extension method:

如果您想减少代码大小,那么只需制作一个扩展方法:

public static class DictionaryExtensions
{
    public static IDictionary<TKey,TVal> Merge<TKey,TVal>(this IDictionary<TKey,TVal> dictA, IDictionary<TKey,TVal> dictB)
    {
        IDictionary<TKey,TVal> output = new Dictionary<TKey,TVal>(dictA);

        foreach (KeyValuePair<TKey,TVal> pair in dictB)
        {
            // TODO: Check for collisions?
            output.Add(pair.Key, Pair.Value);
        }

        return output;
    }
}

Then you can use it by importing ('using') the DictionaryExtensions namespace and writing:

然后你可以通过导入('使用')DictionaryExtensions命名空间并编写来使用它:

IDictionary<string,objet> output = dictA.Merge(dictB);

I have made the method act like the objects are immutable, but you could easily modify it to not return a new dictionary and just merge into dictA.

我已经使方法行为像对象是不可变的,但你可以很容易地修改它不返回一个新的字典,只是合并到dictA。

#4


var result = dictionaries.SelectMany(dict => dict)
             .ToDictionary(pair => pair.Key, pair => pair.Value);

#1


var d3 = d1.Concat(d2).ToDictionary(x => x.Key, x => x.Value);

#2


You can use Concat:

你可以使用Concat:

Dictionary<string, object> d1 = new Dictionary<string, object>();
d1.Add("a", new object());
d1.Add("b", new object());
Dictionary<string, object> d2 = new Dictionary<string, object>();
d2.Add("c", new object());
d2.Add("d", new object());

Dictionary<string, object> d3 = d1.Concat(d2).ToDictionary(e => e.Key, e => e.Value);

foreach (var item in d3)
{
    Console.WriteLine(item.Key);
}

#3


First up, it's not possible without looping. Whether that loop is done in a (extension) method is irrelevent, it still requires a loop.

首先,没有循环就不可能。是否在(扩展)方法中完成该循环是无关紧要的,它仍然需要循环。

I'm actually going to recommend doing it manually. All the other answers given require using two extention methods (Concat - ToDictionary and SelectMany - ToDictionary) and thus looping twice. If you are doing this to optimise your code, it will be faster to do a loop over dictionary B and add it's contents to dictionary A.

我实际上会建议手动完成它。给出的所有其他答案都需要使用两种扩展方法(Concat - ToDictionary和SelectMany - ToDictionary),因此循环两次。如果你这样做是为了优化你的代码,那么在字典B上做一个循环并将它的内容添加到字典A会更快。

Edit: After further investigation, the Concat operation would only occur during the ToDictionary call, but I still think a custom extension method would be more efficient.

编辑:进一步调查后,Concat操作只会在ToDictionary调用期间发生,但我仍然认为自定义扩展方法会更有效。

If you want to reduce your code size, then just make an extension method:

如果您想减少代码大小,那么只需制作一个扩展方法:

public static class DictionaryExtensions
{
    public static IDictionary<TKey,TVal> Merge<TKey,TVal>(this IDictionary<TKey,TVal> dictA, IDictionary<TKey,TVal> dictB)
    {
        IDictionary<TKey,TVal> output = new Dictionary<TKey,TVal>(dictA);

        foreach (KeyValuePair<TKey,TVal> pair in dictB)
        {
            // TODO: Check for collisions?
            output.Add(pair.Key, Pair.Value);
        }

        return output;
    }
}

Then you can use it by importing ('using') the DictionaryExtensions namespace and writing:

然后你可以通过导入('使用')DictionaryExtensions命名空间并编写来使用它:

IDictionary<string,objet> output = dictA.Merge(dictB);

I have made the method act like the objects are immutable, but you could easily modify it to not return a new dictionary and just merge into dictA.

我已经使方法行为像对象是不可变的,但你可以很容易地修改它不返回一个新的字典,只是合并到dictA。

#4


var result = dictionaries.SelectMany(dict => dict)
             .ToDictionary(pair => pair.Key, pair => pair.Value);