Reputation: 7248
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.
Upvotes: 5
Views: 1869
Reputation: 10456
The only problem in your code is that Select returns IEnumerable
.
Convert it to an array:
int[] array = list.Select(obj=>obj.a).ToArray();
Upvotes: 5
Reputation: 23117
You were almost there:
int[] array = list.Select(obj=>obj.a).ToArray();
you need to just add ToArray
at the end
Upvotes: 11