我正在尝试建立一个多选的管理员用户,以便在已分配或未分配之间切换。一切似乎都是正确的,除非它不喜欢我试图将{或1}}的{{1}}属性赋予真或假的地方。
错误是:
运营商' =='不能应用于' string'类型的操作数和' System.Linq.IQueryable'
感谢任何帮助!
IsInRole
答案 0 :(得分:1)
在您的情况下,您正在两次前往db,一次获取Admin RoleId,第二次获取AdminRole = Yes / Now
这只是一次访问db而不是你的情况下的两次查询。
var adminUsers = db.Roles.Where(r => r.Name == "Administrator")
.SelectMany(r => r.Users.Select(u => new UsersInRoleViewModel
{
UserId = u.Id,
DisplayName = u.DisplayName)).toList();
var users = db.Users.Select(u=> new UsersInRoleViewModel
{
UserId = u.Id,
DisplayName = u.DisplayName)).toList();
var adminMultiSelect = new MultiSelectList(users, "Id", "DisplayName", adminUsers);
var model = new UserSelectListsViewModel { AdminUsers = adminMultiSelect, SelectedAdmins = adminUsers, DevUsers = null, PMUsers = null, SubUsers = null };
答案 1 :(得分:0)
管理员ID是ID列表。您需要将其更改为
var adminId = db.Roles.Where(r => r.Name ==" Administrator")。选择(r => r.Id).First()。ToString();
public ActionResult EditRoles()
{
//AdminUsers
var adminId = db.Roles.Where(r => r.Name == "Administrator").Select(r => r.Id).First().ToString();
var users = db.Users.Select(u => new UsersInRoleViewModel
{
UserId = u.Id,
DisplayName = u.DisplayName,
IsInRole = u.Roles.Where(r => adminId == r.RoleId))
});
var adminUsers = users.Where(u => u.IsInRole).Select(n => n.UserId).ToArray();
var adminMultiSelect = new MultiSelectList(users, "Id", "DisplayName", adminUsers);
var model = new UserSelectListsViewModel { AdminUsers = adminMultiSelect, SelectedAdmins = adminUsers, DevUsers = null, PMUsers = null, SubUsers = null };
return View(model);
}