我正在將 Asp.Net .Net5 與物體框架 5 一起使用。我正在嘗試計算基本用戶的數量。表aspnetuser 通過aspnetuserRoles 鏈接表通過多對多關系連接到aspnetRoles。
我有3張桌子
網路用戶
天冬氨酸
aspnetuserRoles = 鏈接表
public int GetNumberOfActiveBasicUsers() { var users = _context.Users .Where(u => u.IsEnabled == true) .Where(u => u.UserName != "AdminUser") .Include(r => r.UserRoles) .ThenInclude(r => r.Role) //not sure what to put here .Count(); return users; }
aspnetuser表
id | username
--------------
1 | Jim
2 | Harry
3 | James
3 | Susan
aspnet角色
id | name
----------
1 | admin
2 | standard
3 | Basic
aspnetuserRoles // 鏈接表
userId | roleId
----------------
1 | 1
2 | 2
3 | 3
4 | 3
由于有 2 個基本用戶,代碼應回傳值為 2。
uj5u.com熱心網友回復:
嘗試這個
return _context.UserRoles
.Where(ur => ur.User.IsEnabled && ur.Role.Name== "Basic")
.Count();
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360039.html
