MilkToast
MilkToast

Reputation: 211

Get properties of objects within an array

So for instance if I've an array of an object with the properties "Distance" and "Origin" is there any quick way of getting an array of just the distance property from that array without having to do:

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

Upvotes: 0

Views: 79

Answers (1)

RBT
RBT

Reputation: 25897

You need to use LINQ projection query as shown below:

//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();

Upvotes: 2

Related Questions