linq的妙用 分组 交换索引

发布时间 2023-04-20 14:40:53作者: 看风景就
///
/// Splits a collection of objects into n pages with an (for example, if I have a list of 45 shoes and say 'shoes.Split(5)' I will now have 4 pages of 10 shoes and 1 page of 5 shoes.
///
/// The type of object the collection should contain.
/// The collection of objects to be divided into subsets.
/// The number of pages this collection should be split into.
/// A subset of this collection of objects, split into n pages.
public static IEnumerable> Split(this IEnumerable superset, int numberOfPages)
{
return superset
.Select((item, index) => new { index, item })
.GroupBy(x => x.index % numberOfPages)
.Select(x => x.Select(y => y.item));
}