使用 UserManager .Net Core 发布过滤用户角色
Issue filtering users on role with UserManager .Net Core
我在按角色过滤用户时遇到问题。仅对查询进行过滤就足够了。
var result =
_userManager.Users.Where(a =>
(a.UserName.Contains(query) || a.Email.Contains(query))
&& !a.Roles.Any(b => b.RoleId == roleId));
我收到的错误是
SqlException: An expression of non-boolean type specified in a context
where a condition is expected, near ')'.
然后我想嗯,嘿,关于角色的信息说导航 属性,也许我需要包括?
var result =
_userManager.Users.Include(a=> a.Roles).Where(a =>
(a.UserName.Contains(query) || a.Email.Contains(query))
&& !a.Roles.Any(b => b.RoleId == roleId));
嗯,它仍然没有像预期的那样工作,但是嘿至少还有另一个错误。
SqlException: An expression of non-boolean type specified in a context
where a condition is expected, near 'ORDER'.
所以现在我不得不使用这个丑陋的解决方法
var result =
_userManager.Users.Include(a=> a.Roles).Where(a =>
(a.UserName.Contains(query) || a.Email.Contains(query))
/*&& !a.Roles.Any(b => b.RoleId == roleId)*/).ToList();
var result2 = _userManager.GetUsersInRoleAsync(_roleManager.FindByIdAsync(roleId).Result.Name).Result.ToList();
return PartialView("Partials/_UserList", result.Except(result2));
通过将应用程序从 RC1 移植到 RC2,这自行解决了
所以我觉得可以肯定地说这是 RC1 中的一个错误。
我在按角色过滤用户时遇到问题。仅对查询进行过滤就足够了。
var result =
_userManager.Users.Where(a =>
(a.UserName.Contains(query) || a.Email.Contains(query))
&& !a.Roles.Any(b => b.RoleId == roleId));
我收到的错误是
SqlException: An expression of non-boolean type specified in a context where a condition is expected, near ')'.
然后我想嗯,嘿,关于角色的信息说导航 属性,也许我需要包括?
var result =
_userManager.Users.Include(a=> a.Roles).Where(a =>
(a.UserName.Contains(query) || a.Email.Contains(query))
&& !a.Roles.Any(b => b.RoleId == roleId));
嗯,它仍然没有像预期的那样工作,但是嘿至少还有另一个错误。
SqlException: An expression of non-boolean type specified in a context where a condition is expected, near 'ORDER'.
所以现在我不得不使用这个丑陋的解决方法
var result =
_userManager.Users.Include(a=> a.Roles).Where(a =>
(a.UserName.Contains(query) || a.Email.Contains(query))
/*&& !a.Roles.Any(b => b.RoleId == roleId)*/).ToList();
var result2 = _userManager.GetUsersInRoleAsync(_roleManager.FindByIdAsync(roleId).Result.Name).Result.ToList();
return PartialView("Partials/_UserList", result.Except(result2));
通过将应用程序从 RC1 移植到 RC2,这自行解决了
所以我觉得可以肯定地说这是 RC1 中的一个错误。