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 @   全玉  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2022-04-20 判断两个IP是否在一个子网(网段)
点击右上角即可分享
微信分享提示