使用C#,我有一个类型为Foo的List,它有一个字符串属性Bar.我想使用Bar属性将此List转换为字符串数组.
有没有一个简单的(LINQ?)方式来做这个而不必循环遍历集合?
Joe*_*orn 12
List<Foo> l = GetMyList();
string[] myStrings = l.Select(i => i.Bar).ToArray();
Run Code Online (Sandbox Code Playgroud)
请注意,像所有linq代码一样,这仍然会循环遍历集合 - 您只是不自己编写循环.
另请注意,您应该避免在最后一刻之前调用.ToArray().你确定IEnumerable在这里不够好吗?