我創建了這個控制器,我需要排除一些用戶 ID。
這是控制器代碼:
public ActionResult Index()
{
var users = _context.sys_users.ToList();
return View(users);
}
如何選擇所有user_ids位置user_id not in (1, 2, 3)?
uj5u.com熱心網友回復:
您可以創建一個帶有要排除的 id 的串列,并檢查它何時不存在于該串列中。
int[] exclude = { 1, 2, 3 };
var users = _context.sys_users.Where(m => !exclude.Contains(m.user_id)).ToList();
uj5u.com熱心網友回復:
您可以使用 LINQ 過濾用戶。
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);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/495006.html
標籤:asp.net-mvc
上一篇:ASP.NETMVC-控制器使用Html.BeginForm()從視圖接收串列錯誤
下一篇:通過分頁提交按鈕發送查詢引數
