-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChunkExtensions.cs
40 lines (36 loc) · 1.6 KB
/
ChunkExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
namespace CapitalSix.Linq.Extensions
{
public static class ChunkExtensions
{
public const int DEFAULT_CHUNK_SIZE = 2000;
/// <summary>
/// Process a list of T in chunks of size {chunkSize}
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="all">The original list of items</param>
/// <param name="chunkAction">Action to perform on each chunk</param>
/// <param name="chunkSize">Size of each chunk</param>
/// <returns>The original list of items</returns>
public static IEnumerable<T> Chunked<T>(this IEnumerable<T> all, Action<IEnumerable<T>> chunkAction, int chunkSize = DEFAULT_CHUNK_SIZE)
{
IEnumerable<IEnumerable<T>> chunks = all.Chunks(chunkSize);
foreach (IEnumerable<T> chunk in chunks)
{
chunkAction(chunk);
}
return all;
}
/// <summary>
/// Split an enumerable set into chunks of size {chunkSize}
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="all">The original list of items</param>
/// <param name="chunkSize">Size of each chunk</param>
/// <returns>A list of chunks</returns>
public static IEnumerable<IEnumerable<T>> Chunks<T>(this IEnumerable<T> all, int chunkSize = DEFAULT_CHUNK_SIZE)
=> all
.Select((item, index) => (Item: item, Index: index))
.GroupBy(chunkItem => chunkItem.Index / chunkSize)
.Select(chunkItem => chunkItem.Select(x => x.Item).ToList());
}
}