我可以使用 LINQ 返回一个 List<Person>
吗?来自List<String>
像下面这样:
// this is not valid code, just to explain the idea
var persons = new string[] {"Tom", "Adam"}.ToList<Person>(str => Name = str);
提前致谢。
最佳答案
var people = new string[] {"Tom", "Adam"}
.Select(str => new Person {Name = str});
这将返回 IEnumerable<Person>
.如果你想要一个列表:
var people = new string[] {"Tom", "Adam"}
.Select(str => new Person {Name = str})
.ToList();
关于c# - 我可以使用 LINQ 从 List<String> 返回 List<Person>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4056705/