Created
March 7, 2018 20:19
-
-
Save vector623/9cf726e029c40de93b6052479e684d1d to your computer and use it in GitHub Desktop.
A customized version of C#'s Aggregate() method, tailored for paged web API results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static TAccumulate AggregateUntil<TSource, TAccumulate>(this IEnumerable<TSource> source, | |
TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> func, Func<TAccumulate,bool> untilFunc) | |
{ | |
if (source == null) | |
{ | |
throw new ArgumentNullException(nameof(source)); | |
} | |
if (func == null) | |
{ | |
throw new ArgumentNullException(nameof(func)); | |
} | |
TAccumulate result = seed; | |
foreach (TSource element in source) | |
{ | |
result = func(result, element); | |
if (untilFunc(result)) | |
{ | |
break; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment