abp vnext 通用仓储 ef core。WhereIf,多条件,动态条件,分页,动态排序等 电脑版发表于:2021/6/18 21:19 #### abp vnext 通用仓储 ef core WhereIf,多条件,分页等 ``` public async Task<List<LandInfoDto>> GetListAsync(PaginationDto input) { var totalCount = (await _landInfoRepository.GetQueryableAsync()) .WhereIf(!string.IsNullOrEmpty(input.Name), x => x.landName.Contains(input.Name) || x.landName == input.Name) .WhereIf(!string.IsNullOrEmpty(input.Description), x => x.remark.Contains(input.Description) || x.remark == input.Description) .Count(); List<LandInfo> landInfos = (await _landInfoRepository.GetQueryableAsync()) .WhereIf(!string.IsNullOrEmpty(input.Name), x => x.landName.Contains(input.Name) || x.landName == input.Name) .WhereIf(!string.IsNullOrEmpty(input.Description), x => x.remark.Contains(input.Description) || x.remark == input.Description) .Skip((input.pageNo - 1) * input.pageSize) .Take(input.pageSize) .ToList(); //把LandInfo转换成LandInfoDto List<LandInfoDto> result = ObjectMapper.Map<List<LandInfo>, List<LandInfoDto>>(landInfos); LandInfo landInfo = new LandInfo(); foreach (LandInfoDto item in result) { DealData(item, landInfo); } return result; } ``` #### 分页也可以用abp vnext里边封装的方法GetPagedListAsync ``` List<LandInfo> landInfos2 = await _landInfoRepository.GetPagedListAsync((paginationDto.pageNo - 1) * paginationDto.pageSize, paginationDto.pageSize, "Id"); ``` **参数其实都一样** 下面是方法的定义看名字就知道了 ``` Task<List<TEntity>> GetPagedListAsync(int skipCount, int maxResultCount, string sorting, bool includeDetails = false, CancellationToken cancellationToken = default); ``` #### 动态排序的话 可以使用System.Linq.Dynamic.Core 这个库 它可以实现IQueryable级别的、基于字符串的动态表达式构建功能,使用方法如下: ``` var resultSingle = queryable.OrderBy<User>("NumberProperty"); var resultSingleDescending = queryable.OrderBy<User>("NumberProperty DESC"); var resultMultiple = queryable.OrderBy<User>("NumberProperty, StringProperty"); ```