gpt4 book ai didi

c# - 如何合并2本词典

转载 作者:行者123 更新时间:2023-11-30 12:47:41 24 4
gpt4 key购买 nike

我必须聚合两个字典。

代码:

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 查询就可以完成这项任务,那就太棒了。

最佳答案

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

//代码

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

关于c# - 如何合并2本词典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16187461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com