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;
        }
    }
}
相关推荐
uzong2 小时前
一次慢接口背后,竟藏着40+种可能!你中过几个
后端·面试·程序员
G探险者3 小时前
滴滴P0级故障背后:互联网公司是如何分级处理线上事故的?
后端
G探险者3 小时前
从 Tomcat 与 Jetty 的对比,聊聊影响一个服务并发能力的关键因素
后端
你的人类朋友3 小时前
“签名”这个概念是非对称加密独有的吗?
前端·后端·安全
幼稚园的山代王4 小时前
go语言了解
开发语言·后端·golang
kkjt01304 小时前
{MySQL查询性能优化索引失效的八大场景与深度解决方案}
后端
Panda__Panda4 小时前
docker项目打包演示项目(数字排序服务)
运维·javascript·python·docker·容器·c#
weixin_456904274 小时前
基于.NET Framework 4.0的串口通信
开发语言·c#·.net
ss2734 小时前
手写MyBatis第107弹:@MapperScan原理与SqlSessionTemplate线程安全机制
java·开发语言·后端·mybatis
橙子家5 小时前
log4net 简介以及简单示例(.net8)
后端