var query = list.Skip(index - 3) // Start at appropriate index .Take(3) // Only consider the next three values .Where(x => x > 5); // Filter appropriately
Personally it seems a little odd to me that the index would be the end point rather than the start point, mind you. You might want to see if other pieces of code would benefit from changing that.
An alternative to
Take would be to include the item index in the
list.Where((x, i) => // x is the list item, i is its index i > (index - 3) && i <= index && // index bounds x > 5 // item condition )
For you specific requirement with a single sub-range of items, I'd go with the
Take, but if you ever need a more complex condition on the item index, you can utilize this
- LINQ - Fetch items from list starting from specific index
- Is there a Linq operation to retrieve specific items from a list of items where the item has a property value for a property which should be unique?
- Get items from list where index not equal to an int using LINQ
- Linq code to get the index of an object in an array from an object within a list
- How to use LINQ to retrieve a list from another entity with a specific number of items
- getting specific x items from list using linq
- Assign a list of items to a specific attributes from another list Linq
- c# Linq or code to extract groups from a single list of source data
- How to generate an index dictionary from a list of items in C# using linq and lambda
- How to select values within a provided index range from a List using LINQ
- Get specific property from all items from the list
- Add items to list from linq var
- Remove items from list that intersect on property using Linq
- Linq - How to select items from a list that contains only items of another list?
- Remove a specific item from a list using LINQ
- LINQ Getting Unique items from a List within a List
- Using Linq and C#, trying to get two lists from a list of master items grouped by two inner lists
- linq remove items from query where any list value is present
- Group items and select specific item from each group with LINQ
- How to generate a unique list of items from another list using LINQ in C#
- Linq - Excluding items from different list types
- Select items from a List where the children contain the items from another List using LINQ
- Linq select strings from list when condition is met and save the index
- C#- Select specific items from a list based on partial intersection with another list (Linq + Lambda)
- getting same type of inherited class items from base class list using linq
- Selecting "custom distinct" items from a List using LINQ
- Using LINQ to get a list of items where the item contains a part of an item from another list
- Linq to select list of items from custom nested list of objects
- Optimized way to select items from a Collection excluding a list of certain member using Linq
- creating Linq to sqlite dbml from DbLinq source code
- C# LINQ IdentityContext Multi Group Join
- LINQ - 'Could not translate expression' with previously used and proven query condition
- System.InvalidOperationException: The specified cast from a materialized 'System.Int32' type to a nullable 'Country' type is not valid
- Parallel.ForEach error HttpContext.Current
- Combining Two Arrays & Projecting
- Assign value to variable using LINQ in SSIS Script task
- How to get One Table data on the basis of ID from another table
- Why Linq Prepend() does not work with List<T>?
- Resolving service in parameterless constructor causes InvalidOperationException in Repository
- Compare two different lists
- How do I select multiple tables in LINQ?
- Return value of one field from Database[ Entity Framework ] [ ASP.NET MVC ]
- What's a good way to parse an XML file into a C# IEnumerable<dynamic>?
- Expression.Call GroupBy then Select and Count()?
- Filter a list<object> by another list<object2> C# LINQ
- LINQ to SQL, Visual Basic - Listbox Receives the Query NOT the Result
- Left outer join linq query object reference not set to an instance of an object
- multiple Groupby on result of GroupBy Linq
- how to count and group by using linq
- Getting unique values from a list of objects with a List<string> as a property