简体   繁体   English

使用UserManager .Net Core过滤用户角色

[英]Issue filtering users on role with UserManager .Net Core

I'm having an issue filtering users on their roles. 我在过滤用户角色时遇到问题。 Filtering only on the query works nicely enough alone. 仅对查询进行过滤可以很好地单独使用。

var result =
    _userManager.Users.Where(a =>
    (a.UserName.Contains(query) || a.Email.Contains(query))
    && !a.Roles.Any(b => b.RoleId == roleId));

The error im getting is 我得到的错误是

SqlException: An expression of non-boolean type specified in a context where a condition is expected, near ')'. SqlException:在期望条件的上下文中指定的非布尔类型的表达式,在')'附近。

Then i thought hmm, hey the info on Roles says navigational property, maybe i need to include? 然后我想嗯,嘿,Roles的信息说导航财产,也许我需要包括?

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));

hmm, it still dont work like expected, but hey atleast there is another error. 嗯,它仍然没有像预期的那样工作,但至少还有另一个错误。

SqlException: An expression of non-boolean type specified in a context where a condition is expected, near 'ORDER'. SqlException:在“ORDER”附近的预期条件的上下文中指定的非布尔类型的表达式。

so now i have to use this ugly workaround 所以现在我必须使用这个丑陋的解决方法

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));

This resolved itself, by porting the application from RC1 to RC2 这通过将应用程序从RC1移植到RC2来解决

So I feel it's safe to say it was a bug in RC1. 所以我觉得可以说这是RC1中的一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM