diff --git a/src/Umbraco.Core/EnumerableExtensions.cs b/src/Umbraco.Core/EnumerableExtensions.cs index 525f75e709..8055b33ab8 100644 --- a/src/Umbraco.Core/EnumerableExtensions.cs +++ b/src/Umbraco.Core/EnumerableExtensions.cs @@ -15,43 +15,13 @@ namespace Umbraco.Core public static IEnumerable> InGroupsOf(this IEnumerable source, int groupSize) { if (source == null) - throw new NullReferenceException("source"); + throw new ArgumentNullException("source"); + if (groupSize <= 0) + throw new ArgumentException("Must be greater than zero.", "groupSize"); - // enumerate the source only once! - return new InGroupsEnumerator(source, groupSize).Groups(); - } - - // this class makes sure that the source is enumerated only ONCE - // which means that when it is enumerated, the actual groups content - // has to be evaluated at the same time, and stored in an array. - private class InGroupsEnumerator - { - private readonly IEnumerator _source; - private readonly int _count; - private bool _mightHaveNext; - - public InGroupsEnumerator(IEnumerable source, int count) - { - _source = source.GetEnumerator(); - _count = count; - _mightHaveNext = true; - } - - public IEnumerable> Groups() - { - while (_mightHaveNext && _source.MoveNext()) - yield return Group().ToArray(); // see note above - } - - private IEnumerable Group() - { - var c = 0; - do - { - yield return _source.Current; - } while (++c < _count && _source.MoveNext()); - _mightHaveNext = c == _count; - } + return source + .Select((x, i) => Tuple.Create(i / groupSize, x)) + .GroupBy(t => t.Item1, t => t.Item2); } /// The distinct by.