Search code examples
c#linqgeneric-collections

LINQ fetching sequence values


The below list providing a collection of integers. My requirement is to return sequence values are like first 1,2,3,4,5. once the sequence gets less than or equal 1. fetching will be stopped. I could use for loop to do this operation, but I need to do this using LINQ Extension method. Thanks

If I pass 1, then the result should be like 1,2,3,4,5

If I pass 2, then the result should be like 2,3,4,5

If I pass 3, then the result should be like 3,4,5

List<int> list=new List<int>();
list.Add(1);---------
list.Add(2);---------
list.Add(3);---------
list.Add(4);---------
list.Add(5);---------
list.Add(1);
list.Add(2);
list.Add(3);

Solution

  • var result = list.TakeWhile((item, index) => item > list[0] || index == 0);