gpt4 book ai didi

c# - SelectMany on { key, values } 的元组 - 在结果中获取键?

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:59 24 4
gpt4 key购买 nike

我有一个 { string Key, IEnumerable<string> Values } 的列表项目并希望将其转换为 { string Key, string Value } 的列表项,使得新列表包含 n每个项目 Key .

我想使用 LINQ 并且想到了使用 SelectMany :

list.SelectMany(item => item.Values)

但是,如您所见,我丢失了 Key有了那个转变。有什么窍门?我想这应该很容易,我只是只见树木不见森林......

最佳答案

我也遇到过这种脑卡住。从评论中添加@PetSerAl 的答案:

list.SelectMany(item => item.Values.DefaultIfEmpty(), (item, value) => Tuple.Create(item.Key, value))

关于c# - SelectMany on { key, values } 的元组 - 在结果中获取键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33876942/

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