0

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;
}
1
  • 3
    Look into LINQ. Essentially, you'd write something like objectArray.Select(o => o.Distance).ToArray(). Commented Jul 27, 2017 at 2:16

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.