I created this controller and I need to exclude some user ids.
This is the controller code:
public ActionResult Index()
{
var users = _context.sys_users.ToList();
return View(users);
}
How to select all user_ids
where user_id not in (1, 2, 3)
?
CodePudding user response:
You can use LINQ to filter the users.
public async Task<ActionResult> Index()
{
var excludedUserIds = new HashSet<int> { 1, 2, 3 }; //or long whatever your IDs are
var users = _context.sys_users.Where(u => !excludedUserIds.Contains(u.user_id)).ToListAsync(); // Use async methods whenever possible in IO methods such as DB access
return View(users);
}
CodePudding user response:
you can create a list with the id to exclude and check when it does not exist in that list.
int[] exclude = { 1, 2, 3 };
var users = _context.sys_users.Where(m => !exclude.Contains(m.user_id)).ToList();