经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
List分组迭代器
来源:cnblogs  作者:史布斯  时间:2018/10/16 9:40:44  对本文有异议

说明:

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


Source

  1. /******************************************************************************
  2. * 名称:List分组迭代器
  3. * 说明:针对长度较大的List对象,可以分组批量进行处理
  4. * 如:长度为1000的List<int>对象,可分为10组,每组100条,对数据进行业务逻辑处理
  5. * 作者:Sybs
  6. * 时间:2018-10-15
  7. * **************************************************************************/
  8. namespace System.Collections.Generic
  9. {
  10. /// <summary>
  11. /// List分组迭代器
  12. /// </summary>
  13. public class ListGroupIterator<T>
  14. {
  15. private int _groupsize = 1;
  16. /// <summary>
  17. /// 分组大小(缺省值为1)
  18. /// </summary>
  19. public int GroupSize
  20. {
  21. get => _groupsize;
  22. set => _groupsize = value < 1 ? 1 : value;
  23. }
  24. /// <summary>
  25. /// List数据源
  26. /// </summary>
  27. public List<T> Source { get; set; }
  28. public ListGroupIterator() { }
  29. public ListGroupIterator(int groupSize) : this(groupSize, null) { }
  30. public ListGroupIterator(List<T> list) : this(1, list) { }
  31. public ListGroupIterator(int groupSize, List<T> list)
  32. {
  33. this.GroupSize = groupSize;
  34. this.Source = list;
  35. }
  36. /// <summary>
  37. /// ListGroupIterator迭代器
  38. /// </summary>
  39. /// <returns></returns>
  40. public IEnumerator<List<T>> GetEnumerator()
  41. {
  42. if (Source?.Count > 0)
  43. {
  44. var ps = Convert.ToInt32(Math.Ceiling(Source.Count * 1.0d / GroupSize));
  45. var model = Source.Count % GroupSize;
  46. for (int i = 0; i < ps; i++)
  47. {
  48. var len = ps - i == 1 && model > 0 ? model : GroupSize;
  49. yield return Source.GetRange(GroupSize * i, len);
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// 将List<T>实例赋值给ListGroupIterator对象
  55. /// </summary>
  56. /// <param name="list"></param>
  57. public static implicit operator ListGroupIterator<T>(List<T> list)
  58. {
  59. return new ListGroupIterator<T> { Source = list };
  60. }
  61. }
  62. }


调用

  1. using System;
  2. using System.Collections.Generic;
  3. namespace Demo
  4. {
  5. class Program
  6. {
  7. static void Main()
  8. {
  9. ListGroupIterator<int> lg1 = new List<int>() { 1, 2, 3, 4, 5 };
  10. ListGroupIterator<int> lg2 = new ListGroupIterator<int>(new List<int> { 1, 2, 3, 4, 5 });
  11. ListGroupIterator<int> lg3 = new ListGroupIterator<int>(3, new List<int>() { 1, 2, 3, 4, 5 });
  12. lg3.Source.AddRange(new List<int>() { 6, 7, 8, 9 });
  13. lg3.GroupSize = 2;
  14. foreach (var item in lg3) { Console.WriteLine(string.Join(",", item)); }
  15. Console.ReadLine();
  16. }
  17. }
  18. }
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号