private PageGridData <Detail> GetDetailPage <Detail>(PageDataOptions options) where Detail : class { //校验查询值,排序字段,分页大小规则待完 PageGridData <Detail> gridData = new PageGridData <Detail>(); if (options.Value == null) { return(gridData); } //主表主键字段 string keyName = typeof(T).GetKeyName(); //生成查询条件 Expression <Func <Detail, bool> > whereExpression = keyName.CreateExpression <Detail>(options.Value, LinqExpressionType.Equal); var queryeable = repository.DbContext.Set <Detail>().Where(whereExpression); gridData.total = queryeable.Count(); //生成排序字段 var sort = (options.Sort ?? typeof(Detail).GetKeyName()).GetExpression <Detail>(); gridData.rows = queryeable .OrderByDescending(sort) .Skip((options.Page - 1) * options.Rows) .Take(options.Rows) .ToList(); return(gridData); }
/// <summary> /// 加载页面数据 /// </summary> /// <param name="loadSingleParameters"></param> /// <returns></returns> public virtual PageGridData <T> GetPageData(PageDataOptions options) { options = ValidatePageOptions(options, out IQueryable <T> queryable); Dictionary <string, QueryOrderBy> orderbyDic = new Dictionary <string, QueryOrderBy>(); options.Sort.Split(',').Select(x => x.Trim()).Where(c => c.Length > 0).ToList().ForEach(s => { string[] sortArr = s.Split(' ').Select(x => x.Trim()).Where(c => c.Length > 0).ToArray(); if (sortArr.Length == 2) { orderbyDic.Add(sortArr[0], sortArr[1].ToLower() == "desc" ? QueryOrderBy.Desc : QueryOrderBy.Asc); } }); PageGridData <T> pageGridData = new PageGridData <T>(); if (QueryRelativeExpression != null) { queryable = QueryRelativeExpression.Invoke(queryable); } if (options.Export) { pageGridData.rows = queryable.GetIQueryableOrderBy(orderbyDic).Take(Limit).ToList(); } else { pageGridData.rows = repository.IQueryablePage(queryable, options.Page, options.Rows, out int rowCount, orderbyDic).ToList(); pageGridData.total = rowCount; } GetPageDataOnExecuted?.Invoke(pageGridData); return(pageGridData); }
/// <summary> ///通用分页查询 /// </summary> /// <param name="options"></param> /// <returns></returns> public virtual PageGridData <T> GetPageData(PageDataOptions options) { Type entityType = typeof(T); var tableName = entityType.Name; var key = entityType.GetKeyProperty().Name; PageGridData <T> pageGridData = new PageGridData <T>(); //ValidatePageOptions var where = new StringBuilder($" (Enable=1 or Enable=0) "); var order = new StringBuilder($"{key} desc"); if (!string.IsNullOrWhiteSpace(options.Sort))//默认排序字段 { var property = entityType.GetProperty(options.Sort); if (property != null) { options.Order ??= "desc"; if (!(options.Order.ToLower() == "desc" || options.Order.ToLower() == "asc")) { options.Order = "desc"; } order = new StringBuilder($"{property.Name} {options.Order}"); } } if (!string.IsNullOrWhiteSpace(options.Wheres)) { try { var searchParametersList = options.Wheres.DeserializeObject <List <SearchParameters> >(); //判断列的数据类型数字,日期的需要判断值的格式是否正确 for (int i = 0; i < searchParametersList.Count; i++) { SearchParameters x = searchParametersList[i]; x.DisplayType = x.DisplayType.GetDBCondition(); if (string.IsNullOrEmpty(x.Value)) { continue; } PropertyInfo property = entityType.GetProperties().Where(c => c.Name.ToUpper() == x.Name.ToUpper()).FirstOrDefault(); if (property != null) { //property.PropertyType == typeof(string) where.Append($" and {property.Name}='{x.Value}' "); } } } catch { } } var query = new StringBuilder($"*"); int totalCount = 0; var list = repository.SqlQueryWithPage <T>(new PageModel { Table = tableName, Order = order.ToString(), Where = where.ToString(), Query = query.ToString(), Pageindex = options.Page, PageSize = options.Rows, }, ref totalCount); pageGridData.rows = list; pageGridData.total = totalCount; return(pageGridData); }