因此,例如,如果我有一个具有属性"距离"和"起源"有什么快速的方法可以从该数组中获取一个只有距离属性的数组,而不必这样做:
float[] distances = new float[objectArray.Length] ();
for (int i; i < objectArray.Length; i++)
{
distances[i] = objectArray[i].Distance;
}
答案 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();