public ActionResult <PageListOutput <Student> > Query(StudentQueryInput queryInput) { PageListOutput <Student> output = new PageListOutput <Student>(); MockDataService service = new MockDataService(); var query = service.GetStudents(); if (!string.IsNullOrWhiteSpace(queryInput.Name)) { query = query.Where(o => o.Name.Contains(queryInput.Name)).ToList(); } if (queryInput.CreateFrom.HasValue) { query = query.Where(o => o.CreateTime >= queryInput.CreateFrom.Value).ToList(); } if (queryInput.CreateTo.HasValue) { query = query.Where(o => o.CreateTime <= queryInput.CreateTo.Value).ToList(); } int skip = (queryInput.Page - 1) * queryInput.PageSize; output.Total = query.Count(); output.Data = query.Skip(skip).Take(queryInput.PageSize).ToList(); return(output); }
public async Task <StudentSimpleDto> Simple(StudentQueryInput input) { //检查传入参数 if (!input.Id.HasValue) { throw new UserFriendlyException("传入Id参数不正确!"); } var result = await _studentRepository.FirstOrDefaultAsync(x => x.Id == input.Id.Value); if (result == null) { throw new UserFriendlyException("该条信息不存在!"); } return(result.MapTo <StudentSimpleDto>()); }
public async Task <PagedResultDto <StudentDto> > Query(StudentQueryInput input) { //验证参数 if (!input.PageSize.HasValue) { throw new UserFriendlyException("传入PageSize参数不正确!"); } if (!input.Start.HasValue) { throw new UserFriendlyException("传入Start参数不正确!"); } //获取总数 var totalcount = await _studentRepository.CountAsync(); //获取查询对象 var query = _studentRepository.GetAll(); //添加关键词条件 if (!string.IsNullOrEmpty(input.KeyWord)) { query = query.Where(x => x.Name.Contains(input.KeyWord) ); } //添加分页条件 query = query.OrderBy(x => x.CreationTime) .Skip(input.Start.Value).Take(input.PageSize.Value); //执行查询 var courses = await Task.FromResult(query.ToList()); //包装为分页输出对象 return(new PagedResultOutput <StudentDto>(totalcount, courses.MapTo <List <StudentDto> >())); }