List grouping iterator

Keywords: C#

Explain:

For large List objects, it can be processed in groups and batches. For example, List objects with length of 1000 can be divided into 10 groups with 100 items in each group, and the data can be processed in business logic.


Source

/******************************************************************************
 * Name: List grouping iterator
 * Note: For long List objects, it can be processed in groups and batches.
 * Such as: 1000 List < int > objects, can be divided into 10 groups, each group of 100, the data for business logic processing
 * Author: Sybs
 * Time: 2018-10-15
 * **************************************************************************/
namespace System.Collections.Generic
{
    /// <summary>
    /// List grouping iterator
    /// </summary>
    public class ListGroupIterator<T>
    {
        private int _groupsize = 1;

        /// <summary>
        /// Group size (default value is 1)
        /// </summary>
        public int GroupSize
        {
            get => _groupsize;
            set => _groupsize = value < 1 ? 1 : value;
        }

        /// <summary>
        /// List data source
        /// </summary>
        public List<T> Source { get; set; }

        public ListGroupIterator() { }
        public ListGroupIterator(int groupSize) : this(groupSize, null) { }
        public ListGroupIterator(List<T> list) : this(1, list) { }
        public ListGroupIterator(int groupSize, List<T> list)
        {
            this.GroupSize = groupSize;
            this.Source = list;
        }

        /// <summary>
        /// ListGroup Iterator Iterator Iterator
        /// </summary>
        /// <returns></returns>
        public IEnumerator<List<T>> GetEnumerator()
        {
            if (Source?.Count > 0)
            {
                var ps = Convert.ToInt32(Math.Ceiling(Source.Count * 1.0d / GroupSize));
                var model = Source.Count % GroupSize;
                for (int i = 0; i < ps; i++)
                {
                    var len = ps - i == 1 && model > 0 ? model : GroupSize;
                    yield return Source.GetRange(GroupSize * i, len);
                }
            }
        }

        /// <summary>
        /// Assign List < T > instance to ListGroupIterator object
        /// </summary>
        /// <param name="list"></param>
        public static implicit operator ListGroupIterator<T>(List<T> list)
        {
            return new ListGroupIterator<T> { Source = list };
        }
    }
}


call

using System;
using System.Collections.Generic;
namespace Demo
{
    class Program
    {
        static void Main()
        {
            ListGroupIterator<int> lg1 = new List<int>() { 1, 2, 3, 4, 5 };
            ListGroupIterator<int> lg2 = new ListGroupIterator<int>(new List<int> { 1, 2, 3, 4, 5 });
            ListGroupIterator<int> lg3 = new ListGroupIterator<int>(3, new List<int>() { 1, 2, 3, 4, 5 });
            
            lg3.Source.AddRange(new List<int>() { 6, 7, 8, 9 });
            lg3.GroupSize = 2;
            foreach (var item in lg3) { Console.WriteLine(string.Join(",", item)); }
            Console.ReadLine();
        }
    }
}

Posted by esthera on Sun, 03 Feb 2019 03:06:15 -0800