补全Batch扩展
This commit is contained in:
parent
f0ab2f2bd9
commit
2b1817bf08
28
Admin.NET/Admin.NET.Core/Utils/EnumerableExtensions.cs
Normal file
28
Admin.NET/Admin.NET.Core/Utils/EnumerableExtensions.cs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user