using System; using System.Globalization; using System.Collections.Generic; using System.Linq; public class Test { private static IEnumerable> SplitDocumentList(IEnumerable allDocuments, int maxMB) { var lists = new List>(); var list = new List(); foreach (doc document in allDocuments) { int totalMB = list.Sum(d => d.sizemb) + document.sizemb; if (totalMB > maxMB) { lists.Add(list); list = new List(); } list.Add(document); } if (list.Count > 0) lists.Add(list); return lists; } public static void Main() { var list = new List() { new doc { file = "dok1", sizemb = 5 }, new doc { file = "dok2", sizemb = 5 }, new doc { file = "dok3", sizemb = 5 }, new doc { file = "dok4", sizemb = 4 }, }; int maxTotalFileSize = 9; IEnumerable> splittedtDocs = SplitDocumentList(list, maxTotalFileSize); foreach(var l in splittedtDocs) Console.WriteLine(string.Join(",",l.Select(d=>d.file).ToArray())); } public class doc { public string file; public int sizemb; } }