说明:

针对长度较大的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();
}
}
}

List分组迭代器的更多相关文章

  1. List分组迭代器 C#--深入理解类型

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

  2. Java Style的C++容器流式处理类

    很久没有上博客园了,最近一段时间,因为工作的关系时间上比较闲,利用闲暇时间重新翻了一下丢弃很久的C++语言.C++从98.11.14.17目前已经也走到了20版本,发生了很多变化,也引入了很多新的语言 ...

  3. Python(四)装饰器、迭代器&生成器、re正则表达式、字符串格式化

    本章内容: 装饰器 迭代器 & 生成器 re 正则表达式 字符串格式化 装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解 ...

  4. Hadoop Mapreduce分区、分组、二次排序过程详解[转]

    原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动   (1)最简单的过程:  map - reduce   (2) ...

  5. 5.python(迭代器,装饰器,生成器,基本算法,正则)

    一,迭代器 1.迭代器  (1)迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,知道所有的元素被访问完结束.迭代器只能往前不会后退.  (2)对于原生支持随机访问的数据结构(如t ...

  6. 【Java】List集合按数量分组

    有时候,我们需要将大的集合按指定的数量分割成若干个小集合.(比如:集合作为SQL中IN的参数,而SQL又有长度限制,所以需要分批分几次进行查询) 虽然此需求感觉不常见,但偶也写过几次类似的方法,故记录 ...

  7. Python标准库:迭代器Itertools

    Infinite Iterators: Iterator Arguments Results Example count() start, [step] start, start+step, star ...

  8. Python学习笔记 (4) :迭代器、生成器、装饰器、递归、正则表达式等

    迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一大优点是 ...

  9. Python学习之路——迭代器、生成器、算法基础、正则

    一.迭代器: 迭代器是访问集合元素的一种方式. 迭代器对象是从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退. 另外,迭代 ...

随机推荐

  1. RabbitMQ双活实践(转)

    有货RabbitMQ双活实践   消息服务中间件在日常工作中用途很多,如业务之间的解耦,其中 RabbitMQ 是比较容易上手且企业使用比较广泛的一种,本文主要介绍有货在使用 RabbitMQ 的一些 ...

  2. github提交表情包

    emoji-list emoji表情列表 目录 人物 自然 事物 地点 符号 人物 :bowtie: :bowtie: :smile: :smile: :laughing: :laughing: :b ...

  3. WPF 动态创建 DataTemplate 及数据绑定

    WPF 动态创建 DataTemplate 及数据绑定 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-22 参考: star ...

  4. Adjacent Bit Counts(uvalive)

    For a string of n bits x1, x2, x3,…, xn, the adjacent bit count of the string (AdjBC(x)) is given by ...

  5. Delphi数据库的三层架构的问题和解决方法

    Delphi数据库的三层架构的问题和解决方法 原创 2014年03月26日 16:26:03 标签: Delphi / 数据库三层架构 / DCOM / DCOMConnection 790 //-- ...

  6. fdsf

    https://blog.csdn.net/chen_2890/article/details/83757022Elasticsearch环境搭建和介绍(Windows) https://blog.c ...

  7. lucene3.0范围查找

    在lucene3.0以上版本中,范围查询也有很大的变化,RangeQuery已经不推荐使用,使用TermRangeQuery和NumericRangeQuery两个替代.TermRangeQuery: ...

  8. Django实现支付宝付款和微信支付

    支付宝支付和微信支付是当今互联网产品常用的功能,我使用Django Rest Framework实现了网页上支付宝支付和微信支付的一个通用服务,提供rpc接口给其他服务,包括获取支付宝支付页面url的 ...

  9. Java如何快速修改Jar包里的文件内容

    需求背景:写了一个实时读取日志文件以及监控的小程序,打包成了Jar包可执行文件,通过我们的web主系统上传到各个服务器,然后调用ssh命令执行.每次上传前都要通过解压缩软件修改或者替换里面的配置文件, ...

  10. MySQL5.6.35部署

    1.下载软件 [root@localhost src]# wget -q http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glib ...