public async Task <ActionResult> Index(UserSort sortOrder = UserSort.NameAsc) { IQueryable <User> users = _userManager.Users; ViewData["NameSort"] = sortOrder == UserSort.NameAsc ? UserSort.NameDesc : UserSort.NameAsc; ViewData["StatusSort"] = sortOrder == UserSort.StatusAsc ? UserSort.StatusDesc : UserSort.StatusAsc; ViewData["RoleSort"] = sortOrder == UserSort.RoleAsc ? UserSort.RoleDesc : UserSort.RoleAsc; ViewData["BlockSort"] = sortOrder == UserSort.BlockAsc ? UserSort.BlockDesc : UserSort.BlockAsc; users = sortOrder switch { UserSort.NameDesc => users.OrderByDescending(s => s.UserName), UserSort.StatusAsc => users.OrderBy(s => s.Status), UserSort.StatusDesc => users.OrderByDescending(s => s.Status), UserSort.RoleAsc => users.OrderBy(s => s.Role), UserSort.RoleDesc => users.OrderByDescending(s => s.Role), UserSort.BlockAsc => users.OrderBy(s => s.Block), UserSort.BlockDesc => users.OrderByDescending(s => s.Block), _ => users.OrderBy(s => s.UserName), }; string role = null; if (User.Identity.Name != null) { User user = await _userManager.FindByNameAsync(User.Identity.Name); role = user.Role; } UsersViewModel model = new UsersViewModel { Users = await users.AsNoTracking().ToListAsync(), Role = role, }; return(View(model)); }