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;
        }
    }
}
相关推荐
林太白18 分钟前
rust18-通知管理模块
后端·rust
一 乐38 分钟前
医疗管理|医院医疗管理系统|基于springboot+vue医疗管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·医疗管理系统
华仔啊1 小时前
SpringBoot 2.x 和 3.x 的核心区别,这些变化你必须知道
java·spring boot·后端
程序员爱钓鱼1 小时前
Python编程实战——Python实用工具与库:Matplotlib数据可视化
前端·后端·python
程序员爱钓鱼1 小时前
Python编程实战 - Python实用工具与库 - requests 与 BeautifulSoup
前端·后端·python
熊小猿1 小时前
Redis 缓存怎么更新?—— 四种模型与一次“迟到的删除”
java·后端·spring
星释1 小时前
Rust 练习册 :掌握文本处理与词频统计
开发语言·后端·rust
万19991 小时前
asp.net core webapi------3.AutoMapper的使用
c#·.netcore