获取数组

时间:2017-07-27 02:12:22

标签: c# arrays properties

因此,例如,如果我有一个具有属性"距离"和"起源"有什么快速的方法可以从该数组中获取一个只有距离属性的数组,而不必这样做:

float[] distances = new float[objectArray.Length] ();
for (int i; i < objectArray.Length; i++)
{
    distances[i] = objectArray[i].Distance;
}

1 个答案:

答案 0 :(得分:2)

您需要使用LINQ投影查询,如下所示:

//use this namespace at the top of your code file
using System.Linq;

//inside your method. Replace the entire code in your post with this.
var distances = objectArray.Select(x => x.Distance).ToArray();