Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut some elements from the middle of the sequence?

Tags:

c#

.net

linq

Is it possible using LINQ expression to skip some elements from the middle of the sequence? I want to Take N elements, then Skip M elements, then take the rest of the sequence. I know that I can write my own function for that but I wonder is it possible to do it using only built-in functions?

like image 902
Poma Avatar asked Oct 16 '25 13:10

Poma


1 Answers

One way is to use the index:

sequence.Select((value, index) => new {value, index})
.Where(x => x.index < startOfRange || x.index > endOfRange)
.Select(x.value);

Or, even simpler: (can't believe I didn't think of that the first time)

sequence.Where((value, index) => index < startOfRange || index > endOfRange)
like image 126
Dennis_E Avatar answered Oct 18 '25 07:10

Dennis_E