Reputation: 4161
For example I want to take next 3 items I do this
private IEnumerable<T> IndexRange<T>(IList<T> source, int start, int end)
{
return source.Skip(start).Take(end);
}
But I need to be able to take also previous 3 items if I need it. How that would be possible? For example: IndexRange(source, 15, 12);
Upvotes: 0
Views: 49
Reputation: 54487
Your code is wrong. It should be:
return source.Skip(start).Take(end - start + 1);
To do what you're asking it could be:
return source.Skip(Math.Min(start, end)).Take(Math.Abs(end - start) + 1);
Note that my code assumes that both start
and end
are inclusive.
Upvotes: 3