说明:

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

Source

/******************************************************************************
* 名称:List分组迭代器
* 说明:针对长度较大的List对象,可以分组批量进行处理
* 如:长度为1000的List<int>对象,可分为10组,每组100条,对数据进行业务逻辑处理
* 作者:Sybs
* 时间:2018-10-15
* **************************************************************************/
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. Unity Mono IDE Setting
  2. Linux Module
  3. 【洛谷P1080】国王游戏
  4. 使用bootstrap和metroui设计的微网站或手机app界面
  5. LinuxI2C核心、总线驱动与设备驱动
  6. 手把手教你用Python爬虫煎蛋妹纸海量图片
  7. Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏
  8. js 只能输入数字和小数点
  9. PHP.12-PHP-设计文件上传类
  10. 【 D3.js 选择集与数据详解 — 5 】 处理模板的应用
  11. ganglia 无数据问题解决
  12. BootStrap 智能表单系列 九 表单图片上传的支持
  13. AspNet.WebAPI.OData.ODataPQ
  14. window批量-6 rem
  15. Unity3d 基本设计开发 原则(提高代码可读性)
  16. Problem : 1012 ( u Calculate e )
  17. 第五次C语言作业
  18. mysql批量更新数据
  19. python垃圾回收机制:引用计数 VS js垃圾回收机制:标记清除
  20. [转]oracle分页用两层循环还是三层循环?

热门文章

  1. 「小程序JAVA实战」小程序页面引用外部wxml通用页面(21)
  2. MySQL 主从同步失败,数据表修复
  3. Redis AOF 全持久化
  4. Linux ALSA声卡驱动之一:ALSA架构简介
  5. linux shell 脚本攻略学习 -- head命令详解, tail命令详解
  6. MYSQL 测试常用语句使用技巧
  7. 673. Number of Longest Increasing Subsequence最长递增子序列的数量
  8. spring+springmvc+mybatis+redis实现缓存
  9. (转)介绍一些.net开源项目
  10. [C++] c pointer