我必须汇总两个词典。
代码:
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);
我现在想要的是一本字典,其中包含代理商的所有元素&amp;对象字典。
您可能认为重复键可能存在问题,但每个键(ID)都是唯一的,因此没有问题。
如果只能通过一个LINQ查询完成此任务,那将非常酷。
答案 0 :(得分: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);