How to combine 2 dictionaries

I need to fill out two dictionaries.

code:

private Dictionary <int, aor.PhysicalObject> agents; private Dictionary <int, aor.PhysicalObject> objects; agents = (from a in log .InitialState .Agents .Agent select a) .ToDictionary(d => Convert.ToInt32(d.id) , d => d as aor.PhysicalObject); objects = (from o in log .InitialState .Objects .Object select o) .ToDictionary(d => Convert.ToInt32(d.id) , d => d as aor.PhysicalObject); 

Now I want this dictionary containing all the elements of the dictionary of agents and objects.

You might think that there might be a problem with duplicate keys, but each key (id) is unique, so there will be no problems.

It would be great if this task could be accomplished with just one LINQ query.

+4
source share
1 answer

If the keys are unique, you can combine the two dictionaries as follows:

//Code

 private Dictionary <int, aor.PhysicalObject> merger; merger = Enumerable .Concat( from a in log .InitialState .Agents .Agent select a , from o in log .InitialState .Objects .Object select o ).ToDictionary(d => Convert.ToInt32(d.id) , d => d as aor.PhysicalObject); 
+1
source

All Articles