[英]Divide list of lists into equal parts in C#
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. 假设我有一个包含各种俱乐部的x区域列表。 If the total amount of clubs are 40 there should be about 10 clubs in each group of clubs.
如果俱乐部的总数为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; }
}
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): 或者更多LINQ Batch(保留俱乐部的顺序):
int batchSize = region.Club.Count / 4 + 1;
var groups = region.Club.Batch(batchSize);
I use a custom extension method that supports indexes in parts. 我使用支持部分索引的自定义扩展方法。 Basically it does the same thing in lazyberezovsky's answer.
基本上它在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();
}
}
You can use it like this: 你可以像这样使用它:
var partitions = regionList.SelectMany(item => item.Club).ToPartition(4);
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.