How to take all but the last element in a sequence using LINQ?

前端 未结 22 1615
南笙
南笙 2020-11-30 02:51

Let\'s say I have a sequence.

IEnumerable sequence = GetSequenceFromExpensiveSource();
// sequence now contains: 0,1,2,3,...,999999,1000000
         


        
22条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 03:31

    As an alternative to creating your own method and in a case the elements order is not important, the next will work:

    var result = sequence.Reverse().Skip(1);
    

提交回复
热议问题