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;
        }
    }
}
相关推荐
张人玉5 分钟前
c#中Random类、DateTime类、String类
开发语言·c#
天上掉下来个程小白40 分钟前
MybatisPlus-06.核心功能-自定义SQL
java·spring boot·后端·sql·微服务·mybatisplus
知了一笑1 小时前
独立开发第二周:构建、执行、规划
java·前端·后端
寻月隐君1 小时前
想用 Rust 开发游戏?这份超详细的入门教程请收好!
后端·rust·github
晴空月明1 小时前
分布式系统高可用性设计 - 缓存策略与数据同步机制
后端
future14121 小时前
游戏开发日记
数据结构·学习·c#
Real_man2 小时前
新物种与新法则:AI重塑开发与产品未来
前端·后端·面试
小马爱打代码2 小时前
Spring Boot:将应用部署到Kubernetes的完整指南
spring boot·后端·kubernetes
卜锦元3 小时前
Go中使用wire进行统一依赖注入管理
开发语言·后端·golang
军训猫猫头3 小时前
3.检查函数 if (!CheckStart()) return 的妙用 C#例子
开发语言·c#