简体   繁体   中英

Select values for a range of given indexes using Linq C#

I have been trying to take a range of elements from a list Where I have the List of indexes.

Note: I can do it easily using a foreach loop but I want it without using a foreach or for loop.

I tried using takeWhile method of linq as follows but they didnot work.

Example :

List<int> ListOfindexes ; // this has the list of indexses for which I need the values
List<int> mainList;

Itried something like this.

mainList.TakeWhile(value => ListofIndexses.Contains(value));//this is not working.

I have been breaking my head on this from 2 days. Help me out.

Thanks in Advance

It sounds like you want:

var results = indexList.Select(index => mainList[index]);

It's not very clear from your question though...

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