I need to get an element from an IEnumerable and then return itself and a range of elements on either side.
So, something like this:
var enumerable = new[] {54, 107, 24, 223, 134, 65, 36, 7342, 812, 96, 106};
var rangeSize = 2;
var range = enumerable.MySelectRange(x => x == 134, rangeSize);
would return something like { 24, 223, 134, 65, 36 }.
(This project uses .Net 3.5)
EDIT
Ok, people seem to be getting hung up on the array of ints.
I’ve changed the example to hopefully make it more clear what I’m after.
Bear in mind that this isn’t necessarily for an IEnumerable<int>, but will actually be an IEnumerable<TSomething>.
This extension method finds the first element in the sequence satisfying a given predicate, and then returns that element along with a certain number of its neighbouring elements. It handles the end cases.