4

我必须汇总两个字典。

代码:

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);

我现在想要的是一本包含代理和对象字典所有元素的字典。

您可能认为重复键可能有问题,但每个键(id)都是唯一的,所以不会有问题。

如果这项任务只能通过一个 LINQ 查询来完成,那就太酷了。

4

1 回答 1

1

如果键是唯一的,您可以按如下方式组合两个字典:

//代码

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);
于 2013-04-24T08:48:38.210 回答