gpt4 book ai didi

c# - 连接两个集合,从第二个集合中获取值(左外连接)

转载 作者:行者123 更新时间:2023-12-02 04:34:44 26 4
gpt4 key购买 nike

假设我有两个 List<KeyValuePair<string, string>> a 和 b 其中 a =

"one", "N",
"two", "N",
"three", "N"

和 b =

"one", "Y"
"two", "N"

我想做一个左外连接,但是从 b 中获取 Value 的值如果 b 中有一个条目,那么结果应该是 =

"one", "Y" <- this value is taken from b
"two", "N",
"three", "N"

我试过进行正常的左外连接,但结果为 Value “三”始终为空字符串

    var res = (from l in a
join r in b on l.Key equals r.Key into lrs
from lr in lrs.DefaultIfEmpty()
select new KeyValuePair<string, string>(l.Key, lr.Value)).ToArray();

最佳答案

这也应该有效:

var res = (from p in b.Concat(a)
group p by p.Key into g
select g.First()).ToArray();

或者用流利的语法:

var res = b.Concat(a).GroupBy(p => p.Key, (k, g) => g.First()).ToArray();

关于c# - 连接两个集合,从第二个集合中获取值(左外连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22211937/

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