1、定义一个用来装载适合所有类的分页结果类
public class PageDataView<T> { private int _TotalNum; public PageDataView() { this._Items = new List<T>(); } public int TotalNum { get { return _TotalNum; } set { _TotalNum = value; } } private IList<T> _Items; public IList<T> Items { get { return _Items; } set { _Items = value; } } public int CurrentPage { get; set; } public int TotalPageCount { get; set; } }
PageDataView的Items一个泛型属性,所以可以适合所有的类,简洁而通用。
下面是dapper通用万能的泛型分页公共类的使用方法:
public static PageDataView<MSys_Admin> GetList(string name, string loginName, int page,int pageSize=10) { PageCriteria criteria = new PageCriteria(); criteria.Condition = "1=1"; if (!string.IsNullOrEmpty(name)) criteria.Condition += string.Format(" and Name like '%{0}%'", name); if (!string.IsNullOrEmpty(loginName)) criteria.Condition += string.Format(" and LoginName like '%{0}%'", loginName); criteria.CurrentPage = page; criteria.Fields = "*"; criteria.PageSize = pageSize; criteria.TableName = "Sys_Admin a"; criteria.PrimaryKey = "UID"; var r = Common.GetPageData<MSys_Admin>(criteria); return r; }
参考网址:
http://www.lanhusoft.com/Article/130.html