EF如何分页最大优化
程序员文章站
2022-07-09 19:06:14
public static class QueryableExtension { public static IQueryable Paged(this IQueryable query, Expression> expression, OrderB ......
public static class queryableextension { public static iqueryable<t> paged<t>(this iqueryable<t> query, expression<func<t, object>> expression, orderbytype orderbytype = orderbytype.desc,int currentpage = 1, int pagesize = 5,int pagecount = 0) { if (currentpage < 1) { currentpage = 1; } var skip = (currentpage - 1) * pagesize; var take = pagesize; orderbytype bytype = orderbytype; var maxpageindex = pagecount % pagesize == 0 ? pagecount / pagesize : pagecount / pagesize + 1; if (currentpage > maxpageindex / 2)//页码大于一半用倒序 { var mod = pagecount % pagesize; var page = (int)math.ceiling((decimal)pagecount / pagesize); if (currentpage * pagesize >= pagecount) { skip = 0; take = mod == 0 ? pagesize : mod; } else { skip = (page - currentpage - 1) * pagesize + mod; } bytype = orderbytype == orderbytype.desc ? orderbytype.asc : orderbytype.desc; } else { bytype = orderbytype == orderbytype.desc ? orderbytype.desc : orderbytype.asc; } if (pagecount > pagesize) { if (bytype == orderbytype.desc) query = query.orderbydescending(expression).skip(skip).take(take); else query = query.orderby(expression).skip(skip).take(take); } return query; } } public enum orderbytype { desc = 2, asc = 1, }
转自:https://www.cnblogs.com/330774495qq/p/12855455.html
上一篇: 自定义Antd Pro 默认元素