补全Batch扩展

This commit is contained in:
lqc 2025-03-10 01:29:39 +08:00
parent f0ab2f2bd9
commit 2b1817bf08

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Admin.NET.Core.Utils;
public static class EnumerableExtensions
{
/// <summary>
/// 将集合分批次处理
/// </summary>
public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> source, int batchSize)
{
var batch = new List<T>(batchSize);
foreach (var item in source)
{
batch.Add(item);
if (batch.Count == batchSize)
{
yield return batch;
batch = new List<T>(batchSize);
}
}
if (batch.Count > 0)
yield return batch;
}
}