简体   繁体   中英

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)

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

try

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM