It's hard for me to explain, so let me show it with pseudo code:
ObjectX
{
int a;
string b;
}
List<ObjectX> list = //some list of objectsX//
int [] array = list.Select(obj=>obj.a);
I want to fill an array of ints with ints from objectsX, using only one line of linq.
You were almost there:
int[] array = list.Select(obj=>obj.a).ToArray();
you need to just add ToArray
at the end
The only problem in your code is that Select returns IEnumerable
.
Convert it to an array:int[] array = list.Select(obj=>obj.a).ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With