andrew4582

IEnumerable Extensions InSetsOf

Jun 3rd, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Common.Extensions
  7. {
  8.     public static class IEnumerableExtensions
  9.     {
  10.         public static IEnumerable<List<T>> InSetsOf<T>(this IEnumerable<T> source, int max)
  11.         {
  12.             List<T> toReturn = new List<T>(max);
  13.             foreach (var item in source)
  14.             {
  15.                 toReturn.Add(item);
  16.                 if (toReturn.Count == max)
  17.                 {
  18.                     yield return toReturn;
  19.                     toReturn = new List<T>(max);
  20.                 }
  21.             }
  22.             if (toReturn.Any())
  23.             {
  24.                 yield return toReturn;
  25.             }
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment