LINQ in sets of

Thanks to this Stackoverflow answer for showing how to easily return LINQ results in sets:

public static IEnumerable<T[]> InSetsOf<T>(this IEnumerable<T> source, int max) {
List<T> toReturn = new List<T>(max);
foreach (var item in source) {
toReturn.Add(item);
if (toReturn.Count == max) {
yield return toReturn.ToArray();
toReturn = new List<T>(max);
}
}
if (toReturn.Any()) {
yield return toReturn.ToArray();
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s