UNIVPLMDataIntegration/Admin.NET/Admin.NET.Core/Utils/EnumerableExtensions.cs

29 lines
722 B
C#
Raw Normal View History

2025-03-10 01:29:39 +08:00
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;
}
}