diff --git a/Admin.NET/Admin.NET.Core/Utils/EnumerableExtensions.cs b/Admin.NET/Admin.NET.Core/Utils/EnumerableExtensions.cs new file mode 100644 index 00000000..5936502f --- /dev/null +++ b/Admin.NET/Admin.NET.Core/Utils/EnumerableExtensions.cs @@ -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 +{ + /// + /// 将集合分批次处理 + /// + public static IEnumerable> Batch(this IEnumerable source, int batchSize) + { + var batch = new List(batchSize); + foreach (var item in source) + { + batch.Add(item); + if (batch.Count == batchSize) + { + yield return batch; + batch = new List(batchSize); + } + } + if (batch.Count > 0) + yield return batch; + } +}