Split List into Sublists with LINQ

前端 未结 30 2444
灰色年华
灰色年华 2020-11-21 06:26

Is there any way I can separate a List into several separate lists of SomeObject, using the item index as the delimiter of each s

30条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 07:03

    So performatic as the Sam Saffron's approach.

    public static IEnumerable> Batch(this IEnumerable source, int size)
    {
        if (source == null) throw new ArgumentNullException(nameof(source));
        if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size), "Size must be greater than zero.");
    
        return BatchImpl(source, size).TakeWhile(x => x.Any());
    }
    
    static IEnumerable> BatchImpl(this IEnumerable source, int size)
    {
        var values = new List();
        var group = 1;
        var disposed = false;
        var e = source.GetEnumerator();
    
        try
        {
            while (!disposed)
            {
                yield return GetBatch(e, values, group, size, () => { e.Dispose(); disposed = true; });
                group++;
            }
        }
        finally
        {
            if (!disposed)
                e.Dispose();
        }
    }
    
    static IEnumerable GetBatch(IEnumerator e, List values, int group, int size, Action dispose)
    {
        var min = (group - 1) * size + 1;
        var max = group * size;
        var hasValue = false;
    
        while (values.Count < min && e.MoveNext())
        {
            values.Add(e.Current);
        }
    
        for (var i = min; i <= max; i++)
        {
            if (i <= values.Count)
            {
                hasValue = true;
            }
            else if (hasValue = e.MoveNext())
            {
                values.Add(e.Current);
            }
            else
            {
                dispose();
            }
    
            if (hasValue)
                yield return values[i - 1];
            else
                yield break;
        }
    }
    

    }

提交回复
热议问题