😃封装一些常见的又是自己喜欢的方式,方便日后复制
复杂等高级查询,参照sqlsugar官网的表格查询 点我访问
/// <summary> /// 获取分页数据 /// </summary> /// <typeparam name="TEntity"></typeparam> /// <param name="exp"></param> /// <param name="lstWhereIF"></param> /// <returns></returns> public async Task<ResPageModel<TEntity>> GetPageDataAsync<TEntity>(Expression<Func<TEntity, bool>> exp, (bool isWhere, Expression<Func<TEntity, bool>> expression)[] lstWhereIF) where TEntity : class, new() { RefAsync<int> total = 0; RefAsync<int> totalPage = 0; int page = 1; int pageSize = 10; ISugarQueryable<TEntity> queryable = Db.Queryable<TEntity>().Where(exp); foreach (var whereIF in lstWhereIF) { queryable.WhereIF(whereIF.isWhere,whereIF.expression); } var pageData = await queryable.ToPageListAsync(page, pageSize, total, totalPage); return new ResPageModel<TEntity> { PageSize = pageSize, DataCount = total, PageCount = totalPage, Data = pageData }; }
public async Task<ResPageDto<List<TData>>> PageDataAsync<TData>(ReqPageDto model, Expression<Func<TData, bool>>? whereExpression = null, Expression<Func<TData, object>>? orderExpression = null, OrderByType type = OrderByType.Asc) where TData : class { RefAsync<int> total = 0; RefAsync<int> totalPage = 0; List<TData>? pageInfo = await _db.Queryable<TData>() .WhereIF(whereExpression!=null, whereExpression) .OrderByIF(orderExpression!=null, orderExpression,type) .ToPageListAsync(model.PageNum, model.PageSize, total, totalPage); return new ResPageDto<List<TData>> { Data = pageInfo, PageNum = model.PageNum, PageSize = model.PageSize, Total = total, TotalPage = totalPage }; }
相关实体类
/// <summary> /// 请求分页数据传输对象 /// </summary> public class ReqPageDto { /// <summary> /// 页码 /// </summary> public int PageNum { get; set; } /// <summary> /// 每页大小 /// </summary> public int PageSize { get; set; } } /// <summary> /// 响应分页数据传输对象 /// </summary> /// <typeparam name="TData">数据类型</typeparam> public class ResPageDto<TData> { /// <summary> /// 总页数 /// </summary> public int TotalPage { get; set; } /// <summary> /// 总记录数 /// </summary> public int Total { get; set; } /// <summary> /// 页码 /// </summary> public int PageNum { get; set; } /// <summary> /// 每页大小 /// </summary> public int PageSize { get; set; } /// <summary> /// 数据 /// </summary> public TData Data { get; set; } }
本文作者:宁骑
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!