Reputation: 1960
In C#, given an array of integers that represents indexes of an array of items, is there a way to get a sub-array of the array of items that correspond to indexes in one step?
int[] indexesArray = {0,2,4,1};
string[] itemsArray = {"hi", "ciao", "yo"," hey","hello"};
string[] result = builtinMagic(itemsArray, indexesArray);
Upvotes: 1
Views: 670
Reputation: 156938
You can simply Select
the index from the indexesArray
and then get the item at that specific index:
string[] result = indexesArray.Select(idx => itemsArray[idx]).ToArray();
Upvotes: 5