c#分页增加顺序编号方法代码


public List<TModel<T>> ConvertList<T>(int pg, int pagesize, List<T> list,string kidName = "KID") { if (pg < 1) { pg = 1; } List<TModel<T>> models = new List<TModel<T>>(); int n = 0; int x = pg * pagesize; if (list.Count < pagesize) { x = pg * pagesize - pagesize + list.Count; } for (int i = (pg - 1) * pagesize; i < x; i++) { TModel<T> model = new TModel<T>(); int m = i + 1; var t = list[n].GetType().GetProperty(kidName); if(t != null) { t.SetValue(list[n], m, null); } model.keyid = m; model.propert = list[n]; models.Add(model); n++; } return models; } public List<T> ConvertList2<T>(int pg, int pagesize, List<T> list, string kidName = "KID") { if (pg < 1) { pg = 1; } List<T> models = new List<T>(); int n = 0; int x = pg * pagesize; if (list.Count < pagesize) { x = pg * pagesize - pagesize + list.Count; } for (int i = (pg - 1) * pagesize; i < x; i++) { int m = i + 1; var t = list[n].GetType().GetProperty(kidName); if (t != null) { t.SetValue(list[n], m, null); } models.Add(list[n]); n++; } return models; } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pg">当前页码</param> /// <param name="pagesize">每页条数</param> /// <param name="list">集合数据</param> /// <param name="pagenum">数据总数</param> /// <param name="kidName">自增列名称</param> /// <returns></returns> public List<TModel<T>> ResvertList<T>(int pg, int pagesize, List<T> list, int pagenum, string kidName = "KID") { if (pg < 1) { pg = 1; } List<TModel<T>> models = new List<TModel<T>>(); int n = 0; int x = pg * pagesize; if (list.Count < pagesize) { x = pg * pagesize - pagesize + list.Count; } for (int i = (pg - 1) * pagesize; i < x; i++) { TModel<T> model = new TModel<T>(); int c = pagenum - i; var t = list[n].GetType().GetProperty(kidName); if (t != null) { t.SetValue(list[n], c, null); } model.keyid = c; model.propert = list[n]; models.Add(model); n++; } return models; } public List<T> ResvertList2<T>(int pg, int pagesize, List<T> list, int pagenum, string kidName = "KID") { if (pg < 1) { pg = 1; } List<T> models = new List<T>(); int n = 0; int x = pg * pagesize; if (list.Count < pagesize) { x = pg * pagesize - pagesize + list.Count; } for (int i = (pg - 1) * pagesize; i < x; i++) { TModel<T> model = new TModel<T>(); int c = pagenum - i; var t = list[n].GetType().GetProperty(kidName); if (t != null) { t.SetValue(list[n], c, null); } models.Add(list[n]); n++; } return models; }

public class TModel<T>
        {
            public int keyid { get; set; }
            public T propert { get; set; }
        }

 

posted @ 2020-12-12 10:30  程序原快递  阅读(379)  评论(0编辑  收藏  举报