ThunD3eR
ThunD3eR

Reputation: 3436

How to select multiple items from list based of index with Linq?

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)

Upvotes: 0

Views: 1122

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

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();

Upvotes: 1

pm100
pm100

Reputation: 50110

try

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

Upvotes: 0

Related Questions