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

前端 未结 22 1411
南笙
南笙 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:28

    This is a general and IMHO elegant solution that will handle all cases correctly:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Program
    {
        public static void Main()
        {
            IEnumerable<int> r = Enumerable.Range(1, 20);
            foreach (int i in r.AllButLast(3))
                Console.WriteLine(i);
    
            Console.ReadKey();
        }
    }
    
    public static class LinqExt
    {
        public static IEnumerable<T> AllButLast<T>(this IEnumerable<T> enumerable, int n = 1)
        {
            using (IEnumerator<T> enumerator = enumerable.GetEnumerator())
            {
                Queue<T> queue = new Queue<T>(n);
    
                for (int i = 0; i < n && enumerator.MoveNext(); i++)
                    queue.Enqueue(enumerator.Current);
    
                while (enumerator.MoveNext())
                {
                    queue.Enqueue(enumerator.Current);
                    yield return queue.Dequeue();
                }
            }
        }
    }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-30 03:32

    If you can get the Count or Length of an enumerable, which in most cases you can, then just Take(n - 1)

    Example with arrays

    int[] arr = new int[] { 1, 2, 3, 4, 5 };
    int[] sub = arr.Take(arr.Length - 1).ToArray();
    

    Example with IEnumerable<T>

    IEnumerable<int> enu = Enumerable.Range(1, 100);
    IEnumerable<int> sub = enu.Take(enu.Count() - 1);
    
    0 讨论(0)
  • 2020-11-30 03:35

    You could write:

    var list = xyz.Select(x=>x.Id).ToList();
    list.RemoveAt(list.Count - 1);
    
    0 讨论(0)
提交回复
热议问题