I have the following list:
List<int> listOfInt = new List<int> {10, 20, 30, 40, 50};
I am now given a list of indices pointing to the values to be extracted from listOfInt:
int[] idxList= new int[] { 2, 4, 1 };
Now, I want to extract all the values from listOfInt by index (as per provided idxList) into another list.
Here is what I came up with, which seems to work, but I would like to get rid of this foreach loop. How can this be done?
List<int> newList= new List<int>();
foreach(var idx in idxList) newList.Add( listOfInt.ElementAt(idx) );
Solution:
{30, 50, 20} will be written into newList