gpt4 book ai didi

C# KeyValuePair List - to 不保留 int 值

转载 作者:行者123 更新时间:2023-11-30 14:13:38 25 4
gpt4 key购买 nike

我的初始化是:

List<string> convertedList = new List<string>();  
List<KeyValuePair<string, int>> originalList = new List<KeyValuePair<string, int>>();

我基本上想用 originalList
中的字符串值填充 convertedList所以如果 originalList 有一些项目:["foo",5],["bar",16],["baz",100],
我想要 convertedList 包含:["foo"],["bar"],["baz"]
到目前为止,我已经尝试过:

for (int i = 0; i <= originalList.Count; i++)
{
convertedList.Add(actions.ToString());
}

但运气不好。
哦,请记住,我是新手,这个问题的答案可能非常明显。
谢谢你帮我!
如果我只想转换前 X 项,我将如何继续?

最佳答案

 List<String> convertedList = originalList.Select(x => x.Key).ToList();

或者:

  convertedList.AddRange(originalList.Select(x => x.Key));

关于C# KeyValuePair<string, int> List - to <string> 不保留 int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13785852/

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