Accepted answer

i guess this would require minimal bound checking - just basic sanity checks. see if this works

ienumerable<double> getwindow(list<double> lst, int index, int windowsize) {
    if(index >= lst.length){
        // throw proper exception
    return lst.skip(index-windowsize).take(math.min(index,windowsize));


if you only want to get one window, your accepted answer looks appropriate. but for iterating more than one window, i would go with something like this:

public static ienumerable<ienumerable<tsource>> window<tsource>(
    this ienumerable<tsource> source, int size)
    var q = new queue<tsource>(size);

    foreach (var value in source)
        if (q.count >= size)
        yield return q;

Related Query

More Query from same tag