public static IEnumerable <List <T> > InSetsOf <T>(this IEnumerable <T> items, int setSize, bool fillPartialSetWithDefaultItems, T defaultItemToFillGroups) { var counter = 0; Func <T, T, bool> keepGoing = (current, previous) => { if (++counter > setSize) { counter = 0; } return(counter != 0); }; foreach (var list in items.Group(keepGoing).Select(set => set.ToList())) { if (list.Count < setSize && fillPartialSetWithDefaultItems) { list.AddRange(Enumerable.Repeat(defaultItemToFillGroups, setSize - list.Count)); } yield return(list); } }