在C#中将列表的列表分成相等的部分

3

我有一个包含对象列表(地区)的列表,其中包含我想根据俱乐部总数分成四个部分的对象列表(俱乐部)。

假设我有一个包含各种俱乐部的x个地区列表。如果俱乐部的总数为40,则每组俱乐部应该有大约10个俱乐部。

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; }
}

你在意顺序吗?这可以通过几个扩展方法很容易地完成,但类名“Region”暗示您可能希望将其按空间分组,这将使这个问题非常不同。 - Robert Rouhani
3个回答

6
您可以使用分组(不保留俱乐部的顺序)
 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();

或者使用MoreLINQ的批处理(保留俱乐部的顺序):
int batchSize = region.Club.Count / 4 + 1;
var groups = region.Club.Batch(batchSize);

1

我使用了一个自定义的扩展方法,支持在部分中使用索引。基本上它做的事情和lazyberezovsky的答案一样。

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();
    }
}

你可以像这样使用它:

var partitions = regionList.SelectMany(item => item.Club).ToPartition(4);

这是否适用于一个地区内的所有俱乐部? 我有一个地区列表,需要根据其中俱乐部的数量将其分为四组。 - Johan Ketels

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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接