gpt4 book ai didi

c# - 组合 Dictionary + Dictionary 来创建 Dictionary

转载 作者:行者123 更新时间:2023-11-30 19:12:55 27 4
gpt4 key购买 nike

有什么很酷的快速方法可以让两个字典创建第三个字典,以内连接方式将第一个字典的键映射到第二个字典的值?

Dictionary<A,B> dic1 = new Dictionary<A,B> {{a1,b1},{a2,b2},{a3,b3}};
Dictionary<B,C> dic2 = new Dictionary<B,C> {{b1,c1},{b2,c2},{b4,c4}};

Dictionary<A,C> dic3 = SomeFunction(dic1,dic2);
// dic3 = {{a1,c1},{a2,c2}}

最佳答案

你可以做这样的事情来加入内在值(value)

Dictionary<int, string> first = new Dictionary<int, string> { {1, "hello"}, {2, "world"}};

Dictionary<string, bool> second =
new Dictionary<string, bool> { { "hello", true }, {"world", false}};

var result = (from f in first
join s in second on f.Value equals s.Key
select new { f.Key, s.Value }).ToDictionary(x => x.Key, y => y.Value);

如果转储结果,您会看到它是一个字典,其值为 {1: true, 2: false}

关于c# - 组合 Dictionary<A,B> + Dictionary<B,C> 来创建 Dictionary<A,C>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5406433/

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