C# List 分页

        // Task 集合
            var taskList = new List<Task>();
            // 待分页的List集合
            var pageList = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            // 每页数量
            var pageSize = 3;
            // 计算页数
            var pageCount = Math.Ceiling(1.0 * pageList.Count / pageSize);
            for (int page = 1; page <= pageCount; page++)
            {
                // 当前页数据
                var nowPageList =pageList.Skip(pageSize * (page - 1)).Take(pageSize);
                // 每 1 页开启一个 Task
                taskList.Add(
                    Task.Run(() =>
                    {
                        foreach (var t in nowPageList)
                        {
                            Console.WriteLine(t);
                        }
                    }
                    ));
            }

            var completeTask = Task.WhenAll(taskList.ToArray()).ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    Console.WriteLine(t.Exception.Message);
                }

                //var exceptions = t.Exception.Flatten().InnerExceptions;
                //foreach (var e in exceptions)
                //{
                //    Console.WriteLine(e.Message);
                //}
            });
            completeTask.Wait();

 

posted @ 2021-05-15 15:52  温故纳新  阅读(1134)  评论(0编辑  收藏  举报