public async Task <IActionResult> Index(KeywordPagedQuery input) { var queryable = _dbContext.Roles.AsQueryable(); if (!string.IsNullOrWhiteSpace(input.Q)) { queryable = queryable.Where( u => $"{u.Name}".Contains(input.Q) || $"{u.Description}".Contains(input.Q)); } var queryResult = await queryable.OrderBy(x => x.CreationTime).AsNoTracking() .ToPagedListAsync(input.GetPage(), input.GetSize()); var itemViewModels = new List <ListRoleItemViewModel>(); foreach (var role in queryResult) { var dto = _mapper.Map <ListRoleItemViewModel>(role); itemViewModels.Add(dto); } var pagedList = new StaticPagedList <ListRoleItemViewModel>(itemViewModels, queryResult.PageNumber, queryResult.PageSize, queryResult.TotalItemCount); var viewModel = new ListRoleViewModel { Roles = pagedList, Keyword = input.Q }; return(View(viewModel)); }
public async Task <IActionResult> Index(KeywordPagedQuery input) { var queryable = _userManager.Users; if (!string.IsNullOrWhiteSpace(input.Q)) { queryable = queryable.Where( u => $"{u.Email}".Contains(input.Q) || $"{u.UserName}".Contains(input.Q) || $"{u.PhoneNumber}".Contains(input.Q) || // Comment: 如果不拼接成字符串报空引用错, Lewis Zou, 2018-12-10 $"{u.FamilyName}{u.GivenName}".Contains(input.Q) || $"{u.GivenName}{u.FamilyName}".Contains(input.Q) ); } var queryResult = await queryable.OrderBy(x => x.CreationTime).AsNoTracking() .ToPagedListAsync(input.GetPage(), input.GetSize()); var userItemViewModels = new List <ListUserItemViewModel>(); foreach (var user in queryResult) { var dto = _mapper.Map <ListUserItemViewModel>(user); dto.IsLockedOut = await _userManager.IsLockedOutAsync(user); //TODO: 需要优化成一次查询 dto.Roles = string.Join("; ", await _userManager.GetRolesAsync(user)); userItemViewModels.Add(dto); } var pagedList = new StaticPagedList <ListUserItemViewModel>(userItemViewModels, queryResult.PageNumber, queryResult.PageSize, queryResult.TotalItemCount); var viewModel = new ListUserViewModel { Users = pagedList, Keyword = input.Q }; return(View(viewModel)); }