Home > Back-end >  How to select multiple items from list based of index with Linq?
How to select multiple items from list based of index with Linq?

Time:01-02

lets say I have an array like this:

var items = [
 0: {},
 1: {},
 2: {},
 3: {},
 4: {},
 5: {}
]

And I know which Items i want to handle since I have figured out their index:

List<int> myIndexes = new List<int> { 0, 3, 5};

I want to get items 0, 3, 5

How would I accomplish this with a LINQ query?

looking for something like:

var myNewItems = items.Something(myIndexes)

CodePudding user response:

If I understand you right, you have a collection of indexes, say

List<int> myIndexes = new List<int> { 0, 3, 5 };

to get corresponging values from items we can query these myIndexes:

var myValues = myIndexes
  .Select(index => items[index])
  .ToList();

CodePudding user response:

try

  myIndexes.Select(i=>myNewItems[i]);
  • Related