c# - 在 C# 中从一个数组转换为另一个数组

标签 c# arrays linq


我在 C# 中有这段代码,它可以将一个数组转换为另一个数组:

IWebElement[] elements = Self.FindChildren()
Step[] steps = new Step[elements.Length];
for (int i = 0; i < elements.Length; i++)
{
   steps[i] = new Step(elements[i]);
}

如何以更短的方式编写它(使用 linq 或 lambda 表达式)?
谢谢
欧麦

最佳答案

Linq 方法

IWebElement[] elements = Self.FindChildren();
Step[] steps = elements.Select(x => new Step(x)).ToArray();

更快但没有 Linq

IWebElement[] elements = Self.FindChildren()
Step[] steps = new Step[elements.Length];
Array.Copy(elements, steps, elements.Length);

关于c# - 在 C# 中从一个数组转换为另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33235885/

相关文章:

c# - Linq 删除值为 null 的位置

php - 如何将字符串/数据库变成变量

arrays - 根据月份确定冬季或夏季时间(UTC 时区)

c# - 使用事件和 BackgroundWorker 更新导致竞争条件的 UI

c# - ASP.NET SQL Server 存储过程返回消息

c++ - 使用 cout 将数组和字符打印到屏幕时出现意外结果

linq - SubSonic 3 和 Linq 参数化查询

c# - LINQ 查找一系列连续数字

c# - 呈现 ViewComponent 返回 HTML 500 错误

c# - (object[,])range.get_Value(XL.XlRangeValueDataType.xlRangeValueDefault) 导致转换错误