D.Giunchi
D.Giunchi

Reputation: 1960

Get a sub array using array of indexes in one step

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

Answers (1)

Patrick Hofman
Patrick Hofman

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

Related Questions