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;
        }
    }
}
相关推荐
ss27337 分钟前
基于Springboot + vue实现的中医院问诊系统
java·spring boot·后端
左灯右行的爱情2 小时前
Redis 缓存并发问题深度解析:击穿、雪崩与穿透防治指南
java·数据库·redis·后端·缓存
南玖yy2 小时前
C++ 成员变量缺省值:引用、const 与自定义类型的初始化规则详解,引用类型和const类型的成员变量自定义类型成员是否可以用缺省值?
c语言·开发语言·c++·后端·架构·c++基础语法
钢铁男儿2 小时前
C#:创建变量和类的实例
c#
不爱总结的麦穗2 小时前
面试常问!Spring七种事务传播行为一文通关
后端·spring·面试
小虚竹3 小时前
claude 3.7,极为均衡的“全能型战士”大模型,国内直接使用
开发语言·后端·claude·claude3.7
Yharim3 小时前
两个客户端如何通过websocket通信
spring boot·后端·websocket
bcbnb3 小时前
iOS 性能调优实战:三款工具横向对比实测(含 Instruments、KeyMob、Xlog)
后端
极客智谷3 小时前
Spring AI应用系列——基于ARK实现多模态模型应用
人工智能·后端
radient3 小时前
Java/Go双修 - Go并发Goroutine与Java对比
java·后端·go