当前位置:网站首页>C# 分页计算 总页数、当前页数据集合
C# 分页计算 总页数、当前页数据集合
2022-06-22 13:10:00 【熊思宇】
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test4
{
class Program
{
static void Main(string[] args)
{
int AllCount = 9;
Console.WriteLine("总个数:" + AllCount);
int page = AllCount / PageCount;
int remainder = AllCount % PageCount;
Console.WriteLine("页数:" + page);
Console.WriteLine("余数:" + remainder);
int totalPage = remainder > 0 ? page + 1 : page;
Console.WriteLine("总页数:" + totalPage);
//获取第二页索引的起点和结束点
int index = 2;
Console.WriteLine(string.Format("获取第{0}页数据",index));
int start = (index * PageCount) - PageCount;
int end = (index * PageCount) - 1;
Console.WriteLine(string.Format("起点:{0},结束点:{1},index:{2}", start, end, index));
Console.WriteLine("========================================");
//添加随机数
Random random = new Random();
for (int i = 0; i < 17; i++)
{
MyList.Add(random.Next(10, 1000).ToString());
}
Print(MyList);
//获取当页数据集合
Console.WriteLine("获取第3页数据");
List<string> myList = GetIndexList(3);
Print(myList);
Console.ReadKey();
}
public static List<string> MyList = new List<string>();
//一页的个数
public static int PageCount = 8;
/// <summary>
/// 获取当页的数据集合
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
public static List<string> GetIndexList(int index)
{
if (MyList.Count <= 0) return null;
int page = MyList.Count / PageCount;
int remainder = MyList.Count % PageCount;
int allPage = remainder > 0 ? page + 1 : page;
if (index > allPage) return null;
int start = (index * PageCount) - PageCount;
int end = (index * PageCount) - 1;
if (end > MyList.Count) end = MyList.Count -1;
List<string> list = new List<string>();
for (int i = start; i <= end; i++)
{
list.Add(MyList[i]);
}
if(list.Count > 0)
return list;
return null;
}
public static void Print(List<string> list)
{
if (list == null || list.Count <= 0) return;
string msg = string.Empty;
for (int i = 0; i < list.Count; i++)
{
msg += list[i] + ",";
}
Console.WriteLine(msg);
}
}
}
输出结果:
总个数:9
页数:1
余数:1
总页数:2
获取第2页数据
起点:8,结束点:15,index:2
====
393,318,19,71,597,684,59,892,134,773,657,32,264,231,296,257,733,
获取第3页数据
733,
注意===线下面的数据是随机数,每次运行结果不一样,
获取的是第3页,而当前总个数是17,一页是8个,第3页刚好是最后一个,可以看到,计算没有问题
end
边栏推荐
- 《Kubernetes监控篇:Grafana通过自动化方式添加datasource和dashboard》
- Database employment consulting system for your help
- Chip silicon and streaming technology
- CVE-2022-22965复现
- Record of problems encountered in dual network card binding
- Should programmers choose software talent outsourcing companies?
- Query escape in Oracle expdp export
- Query rewriting for opengauss kernel analysis
- Go Web 编程入门:验证器
- MySQL如何让一个表中可以有多个自增列
猜你喜欢

坚持了 10 年的 9 个编程好习惯

How to protect WordPress websites from cyber attacks? It is important to take safety measures

Istio服务网格中的流量复制

JasperReport报表生成工具的基本使用和常见问题

transformers VIT图像模型向量获取

技术实践 | 场景导向的音视频通话体验优化

3dMax建模笔记(一):介绍3dMax和创建第一个模型Hello world

史蒂芬·柯维写给年轻人的高效工作秘笈

Getting started with go web programming: validators

Technology practice | scene oriented audio and video call experience Optimization
随机推荐
Installing and using protobuf-c
SQL Server common functions
JS高级程序设计第 4 版:迭代器的学习
History of hash index design
How do I open an account on my mobile phone? Is it safe to open an account online?
Neuron+ekuiper realizes data collection, cleaning and anti control of industrial Internet of things
Oracle user space statistics
30岁之前一定要明白的道理
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
MySQL如何让一个表中可以有多个自增列
Do you know the scope and process of software project acceptance testing?
Interpretation of the thesis -- factorization meets the neighborhood: a multifaceted collaborative filtering model
Configuring cplex12.4 tutorial in VS2010
Tianrun cloud is about to be listed: VC tycoon Tian Suning significantly reduces his holdings and is expected to cash out HK $260million
Basic usage and FAQs of jasperreport report report generation tool
C#定义和实现Interface接口
Chapter 1 overview of naturallanguageprocessing and deep learning
Traffic replication in istio Service Grid
Quickly understand the commonly used symmetric encryption algorithm, and no longer have to worry about the interviewer's thorough inquiry
毕业论文写作中致谢词的常见写法及优秀范文