using System.Collections.Generic;
public static void Main()
var records = new List<Foo>() {
var groups = Group(records, 3);
for (int i = 0; i < groups.Count(); ++i) {
System.Console.WriteLine($"Group {i}: {Newtonsoft.Json.JsonConvert.SerializeObject(groups[i])}");
public Foo(int id, int groupId) {
public static List<List<Foo>> Group(IEnumerable<Foo> records, int maxGroupSize)
var rv = new List<List<Foo>>();
foreach (var record in records) {
for (int i = 0; i < rv.Count; ++i) {
if (rv[i].Count < maxGroupSize && !rv[i].Any(a => a.GroupId == record.GroupId)) {
rv.Add(new List<Foo>() { record });