Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ: Split list into groups according to weight/size

Tags:

c#

linq

I've many examples using LINQ how to divide a list into sub-list according to max items in each list. But In this case I'm interested in diving a sub-lists using sizemb as a weight - having a max total filesize per list of 9mb.

    public class doc
    {
        public string file;
        public int sizemb;
    }

    var list = new List<doc>()
    {
         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;

The above list should then be divided into 3 lists. If any 'files' are more than 9mb they should be in their own list.

I made a non LINQ-version here:

        var lists = new List<List<doc>>();
        foreach (var item in list)
        {
            //Try and place the document into a sub-list
            var availableSlot = lists.FirstOrDefault(p => (p.Sum(x => x.sizemb) + item.sizemb) < maxGroupSize);
            if (availableSlot == null)
                lists.Add(new List<doc>() { item });
            else
                availableSlot.Add(item);
        }
like image 326
bluee Avatar asked May 17 '13 07:05

bluee


1 Answers

You could use this method:

IEnumerable<IList<doc>> SplitDocumentList(IEnumerable<doc> allDocuments, int maxMB)
{
    var lists = new List<IList<doc>>();
    var list = new List<doc>();
    foreach (doc document in allDocuments)
    {
        int totalMB = list.Sum(d => d.sizemb) + document.sizemb;
        if (totalMB > maxMB)
        {
            lists.Add(list);
            list = new List<doc>();
        }
        list.Add(document);
    }
    if (list.Count > 0)
        lists.Add(list);
    return lists;
}

Here's a demo: http://ideone.com/OkXw7C

dok1
dok2
dok3,dok4
like image 119
Tim Schmelter Avatar answered Sep 19 '22 10:09

Tim Schmelter