gpt4 book ai didi

c# - 从元组列表到对象列表的选择 C#

转载 作者:行者123 更新时间:2023-11-30 22:09:08 25 4
gpt4 key购买 nike

在我的 C# 代码中,我有一个列表 List<Tuple<int,string>> .我想选择/将其转换为 List<Type> .我想避免迭代我的元组列表并插入其他列表。有什么办法吗?也许用 LINQ?

最佳答案

您不能更改列表的类型。您只能创建另一种类型的新列表,并用列表中的转换值填充它。我建议使用 List<T>.ConvertAll 正是为此目的而存在的方法:

List<Tuple<int, string>> tuples = new List<Tuple<int, string>>();
// ...
List<YourType> types =
tuples.ConvertAll(t => new YourType { Foo = t.Item1, Bar = t.Item2 });

关于c# - 从元组列表到对象列表的选择 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21726520/

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