I've got a List with n Foo items. Foo contains a long property Foo.FileSize. Now i would like to split this list to sublists with n elements which the sum of FileSize is not more than 10000. Of cource there are items with Foo.FileSize more than 10000 as well. For this special case, just need a sublist with only this item.
Please can someone suggest something?
const long maxdownloadsize = 10485760;
long actualdownloadsize = 0;
List<TI> downloadTI = new List<TI>();
for (int i = 0; i < comparedTI.Count; i++)
{
var ti = comparedTI[i];
actualdownloadsize += ti.FileSize;
downloadTI.Add(ti);
if (actualdownloadsize > maxdownloadsize || i == comparedTI.Count-1)
{
actualdownloadsize = 0;
AddToList(downloadTI);
downloadTI = new List<TI>();
}
}
Honestly it's easier to do this in a traditional manner rather than with Linq, since the transformation method required for each element in the sequence requires knowledge of all previous elements. So, you could do something like this:
And then call it like:
While this meets the requirement of "Split
List<T>
intoSublists<T>
with LINQ", it is actually less efficient than doing it directly.