3

I have a list of objects(Regions) containing lists of objects(Clubs) that I'd like to divide into four parts based on the total number of clubs.

Lets say I have a list of x Regions containing a various number of Clubs. If the total amount of clubs are 40 there should be about 10 clubs in each group of clubs.

public class Club
{
    public string Name { get; set; }
    public int ID { get; set; }
}

public class Region
{
    public string Name { get; set; }
    public List<Club> Club { get; set; }
}
Johan Ketels
  • 167
  • 1
  • 9
  • Do you care about the order? This can be done pretty easily with a few extension methods, but the class name `Region` implies you might want it to be grouped spatially, which would make this question very different. – Robert Rouhani Jul 16 '13 at 09:20
  • possible duplicate of [LINQ Partition List into Lists of 8 members](http://stackoverflow.com/questions/3773403/linq-partition-list-into-lists-of-8-members) – nawfal Jul 16 '13 at 10:24

3 Answers3

6

You can use grouping (does not preserve order of clubs)

 List<IEnumerable<Club>> groups = region.Club.Select((c,i) => new {c,i})
                                             .GroupBy(x => x.i % 4)
                                             .Select(g => g.Select(x => x.c))
                                             .ToList();

Or MoreLINQ Batch (preserves order of clubs):

int batchSize = region.Club.Count / 4 + 1;
var groups = region.Club.Batch(batchSize);
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
1

I use a custom extension method that supports indexes in parts. Basically it does the same thing in lazyberezovsky's answer.

public static class PartitionExtensions
{
    public static IEnumerable<IPartition<T>> ToPartition<T>(this IEnumerable<T> source, int partitionCount)
    {
        if (source == null)
        {
            throw new NullReferenceException("source");
        }

        return source.Select((item, index) => new { Value = item, Index = index })
                     .GroupBy(item => item.Index % partitionCount)
                     .Select(group => new Partition<T>(group.Key, group.Select(item => item.Value)));
    }
}

public interface IPartition<out T> : IEnumerable<T>
{
    int Index { get; }
}

public class Partition<T> : IPartition<T>
{
    private readonly IEnumerable<T> _values;

    public Partition(int index, IEnumerable<T> values)
    {
        Index = index;
        _values = values;
    }

    public int Index { get; private set; }

    public IEnumerator<T> GetEnumerator()
    {
        return _values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

You can use it like this:

var partitions = regionList.SelectMany(item => item.Club).ToPartition(4);
Ufuk Hacıoğulları
  • 37,978
  • 12
  • 114
  • 156
  • Doesn't this apply to all Clubs in one region? I have a list of Regions that I need to group into four based on the number of Clubs inside them. – Johan Ketels Jul 16 '13 at 10:09
0
public static class BatchingExtensions
{
    public static IEnumerable<List<T>> InBatches<T>(this IEnumerable<T> items, int length)
    {
        var list = new List<T>(length);
        foreach (var item in items)
        {
            list.Add(item);
            if (list.Count == length)
            {
                yield return list;
                list = new List<T>(length);
            }
        }
        if (list.Any())
            yield return list;
    }
}
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121