28 lines
642 B
C#
28 lines
642 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Auth.Core.Utility;
|
|
|
|
public static class IEnumerableExtensions
|
|
{
|
|
public static IEnumerable<List<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;
|
|
}
|
|
}
|
|
}
|