score:3
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));
}
score:0
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)
q.Dequeue();
q.Enqueue(value);
yield return q;
}
}
Source: stackoverflow.com
Related Articles
- List View Sliding Window
- Create a sliding window from a list
- List or Array of String Contain specific word in Html Source Code
- c# Linq or code to extract groups from a single list of source data
- Accessing Results View from variable in code
- Generating the Shortest Regex Dynamically from a source List of Strings
- Where can I view LINQ source code?
- Calculate max on a sliding window for TimeSeries
- LINQ Source Code Available
- How to add data from a List<List<String>> to a list view
- .NET 4 Code Contracts: "requires unproven: source != null"
- LINQ query returns old results when source list is re-initialized
- Copy a list of objects to another typed list in one line of code
- C# - Linq optimize code with List and Where clause
- How to Pass a List of Items from View to Controller (ASP.NET MVC 4)
- List and create in same view in asp.net mvc
- creating Linq to sqlite dbml from DbLinq source code
- MVC pass viewmodel view with viewmodel containing parent values and child list
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
- Displaying list of values from a list view into body of an email
- How to group product by category on list view by LinQ?
- Code to apply expression tree directly to List
- Getting wrong data from SQL view when putting it into a C# list
- Search by zip code and filter list within specific radius?
- I can't get pagination to show up in every page on a GridView with a list as source
- Mapping View to Entity using EF 5 Code First
- Accessing a List multiple times in same code block - Any better approach?
- Code practice to handle empty result set in Linq to custom list
- Code First EF: While searching database table is it possible to retrieve list of items it has in DataModel?
- select from list which has same code
- What the linq query for SQL like and Soudex for sql server 2008?
- How do I update a table with LINQ-to-SQL without having to delete all the existing records?
- How can I bind a listbox selecteditem content to a textbox?
- Grouping IDataRecord individual records to a collection
- .NET - comma separated appSettings value to list of integers (+ handle empty value)
- LINQ - EF Core - Return object that contains two objects (referenced by property) in nested list
- Append property value to c# model value using LINQ
- Using contains via LINQ query
- LINQ how to project a list while combining properties of object
- The famous old issue of LINQ to Entities queries with filter on nullable fields
- How can I get totals for more than one column in a list and put this into an object using LINQ?
- How do I find the count of overlapping properties in SQL/LINQ query between two tables?
- List with in List using Lambda
- Entity Framework T-Sql "having" Equivalent
- Concatenate string members using Linq PredicateBuilder for text search
- Linq version of SQL "IN" statement
- check a string array from a list contains a string from another list or vice versa
- IEnumerable add item?
- How can I get my ajax request date format to match my database date format?
- Linq query - Join 2 tables