Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How would I accomplish this with a LINQ query?

looking for something like:

var myNewItems = items.Something(myIndexes)

>Solution :

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading