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 Query
- 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
More Query from same tag
- How to find total number of childs and Number of grand child levels under a hierarchical list parent in C#
- MissingMetadataException when building UWP app with .Net native
- how to use Foreign key in L2E or EF when needs insert?
- entity framework and LINQ - Arithmetic operation resulted in an overflow
- LINQ equivalent of SQL IsNull(..,..) in the Where Clause
- Caching LINQ expressions by equality
- Entity Framework group by age range starting at zero
- "System.ArgumentOutOfRangeException" why is it showing? And why does the list<>number stop printing?
- Return a parent entity together with its child entities using LINQ method syntax
- I want to join two tables and want column of both tables in result using linq
- How can I return all the columns of a table using multiple distinct columns in Linq
- Why this LINQ throws NullReferenceException?
- Could/should these these three queries be combined into one?
- Pivot in LINQ using lambda expression
- Can I use Expression<Func<T, bool>> and reliably see which properties are referenced in the Func<T, bool>?
- How to use LINQ Group Records and use Distinct
- List of strings into Bson Array
- Dependency injection causing poor performance (ASP.NET MVC)
- Linq Paging - How to incorporate total record count
- Merging two tables using criteria with linq
- .First does not throw exception on empty collection
- Sort a list by a string property, Where the string is of a Nullable decimal
- retrieve last value from table in linq Extention method and bind with DataGridView
- Linq DateTime comparison not working
- Populating ComboBox Items with members from a dynamic database, C#
- Using Anonymous Type from LINQ to SQL
- The concise way to initialize an array of reference type object
- Create a User with aspnet schema and UserRepository/ EF ASPNET MVC 4
- MongoDB C# driver fast on take(1) but slow on take(2)
- Handling null values and missing columns in Linq-to-Xml