/// <summary> /// Proccess the task asynchronously. /// </summary> /// <typeparam name="TValue">The type of the value.</typeparam> /// <typeparam name="TContext">The type of the context.</typeparam> /// <param name="repository">The repository.</param> /// <param name="list">The list.</param> /// <param name="chunkSize">Size of the chunk.</param> /// <param name="task">The task.</param> /// <param name="className">Name of the class.</param> /// <param name="token">The token.</param> internal static async Task ProccessTaskAsync <TValue, TContext>( IBaseRepositoryAsync <TValue, TContext> repository, IEnumerable <TValue> list, int chunkSize, Func <IEnumerable <TValue>, CancellationToken, Task> task, string className, CancellationToken token) where TValue : class where TContext : DbContext { ThrowErrorIf.IsNullValue(repository, nameof(repository), className); ThrowErrorIf.IsNullOrEmptyList(list, nameof(list), className); ThrowErrorIf.IsLessThanOrEqualsZero(chunkSize); await repository.MultiTransactionsAsync( async ctx => { var listSplit = list.SplitList(chunkSize); foreach (var split in listSplit) { await task(split, token). ConfigureAwait(false); } }, token).ConfigureAwait(false); }
/// <summary> /// Splits the list. /// </summary> /// <typeparam name="TValue">The type of the value.</typeparam> /// <param name="list">The list.</param> /// <param name="size">The size.</param> /// <returns></returns> public static IEnumerable <IEnumerable <TValue> > SplitList <TValue>(this IEnumerable <TValue> list, int size) { ThrowErrorIf. IsNullOrEmptyList(list, nameof(list), nameof(SplitList)); ThrowErrorIf. IsLessThanOrEqualsZero(size); var listSplited = list.Select((x, i) => new { Index = i, Value = x }) .GroupBy(x => x.Index / size) .Select(x => x.Select(v => v.Value)); return(listSplited); }
public void ThrowErrorIf_IsEqualsZero() => Assert.Throws <LessThanOrEqualsZeroException>(() => { ThrowErrorIf.IsLessThanOrEqualsZero(0); } );