利用linq的Take Skip方法解决 集合拆分的问题
public static void Main(string[] args) { List<string> list = new List<string>(); for (int i = 0; i < 99; i++) { list.Add(i.ToString()); } Console.WriteLine("集合的长度:"+" "+list.Count); Console.ReadKey(); List<List<string>> bigList = new List<List<string>>(); int pageSize = 10; int allPage = (list.Count-1) / pageSize + 1; //int curPage = 1; for (int curPage = 1; curPage <= allPage; curPage++) { List<string> smallList = new List<string>(); smallList = list.Take(pageSize * curPage).Skip(pageSize*(curPage - 1)).ToList(); bigList.Add(smallList); }
1 //另外一种写法 2 3 for (int curPage = 0; curPage < allPage; curPage++) 4 { 5 List<string> smllList = list.Skip(pageSize * i).Take(pageSize).ToList(); 6 bigList.Add(smallList); 7 }
Console.WriteLine("bigList 的容量" + bigList.Count); for (int i = 0; i<bigList.Count;i++) { Console.WriteLine(string.Format("bigList[{0}]", i)+"的容量为: " + bigList[i].Count.ToString()); } Console.ReadKey(); }