-2

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);
sebu
  • 2,824
  • 1
  • 30
  • 45
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/184841/discussion-on-question-by-sebu-linq-fetching-sequence-values). – Samuel Liew Dec 06 '18 at 22:42

2 Answers2

3
var result = list.TakeWhile((item, index) => item > list[0] || index == 0);
Magnus
  • 45,362
  • 8
  • 80
  • 118
0

You can simply use a loop:

List<int> list2 = new List<int>()
for(int i=0;i<list.Count;i++)
    if(i==0 || list[i]>list2[i-1])
        list2.Add(list[i]);
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171