With the help of LINQ, I need to fetch items from a list based on a condition. For that it should consider items only from (provided index - 3)
to provided index (dynamically).
For example, a list contains items {1,3,5,7,9,11,13}
. If provided index is 4, the it should consider total three indexes starting from index 2 to ending at index 4. Among these three items, it should be filter them with a condition - say, Item should be greater than 5.
The result should be - {7,9}
What I tried is, which is wrong and I am stuck:
list.Select(item => list.Select(index => item[index - 3] && item > 5).ToList());
It sounds like you just want a mix of Skip
and Take
, filtering with Where
:
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.
See more on this question at Stackoverflow