C# 切割数组的Linq扩展方法 Period,PeriodBy

1. 以指定条件为周期的列表枚举, 遍历一个列表

cs 复制代码
/// <summary>
/// 以指定条件为周期的列表枚举, 遍历一个列表<br/>
/// 2025-4-1 Ciaran
/// </summary>
public static IEnumerable<List<TSource>> PeriodBy<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> checkNext)
{
    // 2023-1-3 Ciaran 读取以 指定数量为一个周期, 处理对应数量内的列表
    using (var enumerator = source.GetEnumerator())
    {
        while (true)
        {
            List<TSource> list = new List<TSource>();
            while (enumerator.MoveNext())
            {
                var value = enumerator.Current;
                if (checkNext(value) && list.Count > 0)
                {
                    yield return list;
                    list = new List<TSource>();
                }

                list.Add(value);
            }
            if (list.Count == 0)
            {
                yield break;
            }

            yield return list;
        }
    }
}

1.1 PeriodBy 测试代码

csharp 复制代码
[TestClass]
public class MsgTest
{
    
    [TestMethod]
    public void test1()
    {
        int char_count = 0;
        int MAX_CONTENT_LEN = 10;
        bool CheckNext(string x)
        {
            if (char_count + x.Length > MAX_CONTENT_LEN)
            {
                char_count = x.Length;
                return true;
            }

            char_count += x.Length;
            return false;
        }

        var msgs = new List<string>
        {
            "AAAAAAAAAAA","BB","CCC","DD","EEEEE","FF","GGGGG","HH","IIIII"
        };

        var new_msgs = new List<string>();

        foreach (var msgs1 in msgs.PeriodBy(CheckNext))
        {
            var sb = new StringBuilder();
            foreach (var m1 in msgs1)
            {
                sb.Append(m1);
            }
            new_msgs.Add(sb.ToString());
        }

        /*
         * 0|AAAAAAAAAAA
         * 1|BBCCCDD
         * 2|EEEEEFF
         * 3|GGGGGHH
         * 4|IIIII
         */
    }
}

2、以指定数量为长度周期的列表枚举, 遍历一个列表

cs 复制代码
/// <summary>
/// 以指定数量为长度周期的列表枚举, 遍历一个列表<br/>
/// 2023-1-3 Ciaran
/// </summary>
public static IEnumerable<List<TSource>> Period<TSource>(this IEnumerable<TSource> source, int count)
{
    if (count <= 0)
    {
        throw new ArgumentOutOfRangeException(nameof(count));
    }

    // 2023-1-3 Ciaran 读取以 指定数量为一个周期, 处理对应数量内的列表
    using (var enumerator = source.GetEnumerator())
    {
        while (true)
        {
            List<TSource> list = new List<TSource>(count);
            for (int i = 0; i < count; i++)
            {
                if (enumerator.MoveNext())
                {
                    list.Add(enumerator.Current);
                }
                else
                {
                    break;
                }
            }

            if (list.Count == 0)
            {
                yield break;
            }

            yield return list;
        }
    }
}
相关推荐
绝不收费—免费看不了了联系我12 分钟前
Fastapi的单进程响应问题 和 解决方法
开发语言·后端·python·fastapi
喵个咪25 分钟前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:OPA 集成指南:从原理到实践
后端·go
Victor3561 小时前
Netty(11) Netty的心跳机制是什么?为什么需要它?
后端
Victor3561 小时前
Netty(12)Netty支持哪些协议和传输方式?
后端
无限大61 小时前
为什么电脑需要"内存"和"硬盘"?——存储金字塔的秘密
后端
ovensi2 小时前
Docker+NestJS+ELK:从零搭建全链路日志监控系统
后端·nestjs
武子康3 小时前
大数据-184 Elasticsearch Doc Values 机制详解:列式存储如何支撑排序/聚合/脚本
大数据·后端·elasticsearch
四月__3 小时前
http八股
后端
沐森3 小时前
rust并发
后端
喵个咪3 小时前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:Casbin集成指南
后端·go