List分组迭代器

文章目录

针对长度较大的List对象,可以分组批量进行处理, 如:长度为1000的List对象,可分为10组,每组100条,对数据进行业务逻辑处理…


代码(c#):

/**
 * 名称:List分组迭代器
 * 说明:针对长度较大的List对象,可以分组批量进行处理
 * 如:长度为1000的List<int>对象,可分为10组,每组100条,对数据进行业务逻辑处理
**/
namespace System.Collections.Generic
{
    /// <summary>
    /// List分组迭代器
    /// </summary>
    public class ListGroupIterator<T>
    {
        private int _groupsize = 1;

        /// <summary>
        /// 分组大小(缺省值为1)
        /// </summary>
        public int GroupSize
        {
            get => _groupsize;
            set => _groupsize = value < 1 ? 1 : value;
        }

        /// <summary>
        /// 分组数量
        /// </summary>
        public int GroupCount { get => (Source.Count + GroupSize - 1) / GroupSize; }

        /// <summary>
        /// List数据源
        /// </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>
        /// ListGroupIterator迭代器
        /// </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>
        /// 将List<T>实例赋值给ListGroupIterator对象
        /// </summary>
        /// <param name="list"></param>
        public static implicit operator ListGroupIterator<T>(List<T> list)
        {
            return new ListGroupIterator<T> { Source = list };
        }
    }
}


调用:

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();
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值