Skip to content

Instantly share code, notes, and snippets.

@vector623
Created March 7, 2018 20:19
Show Gist options
  • Save vector623/9cf726e029c40de93b6052479e684d1d to your computer and use it in GitHub Desktop.
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
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