linq的妙用 分组 交换索引

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

 

posted @ 2023-04-20 14:32  全玉  阅读(10)  评论(0编辑  收藏  举报