Search code examples
c#asp.netasp.net-coreasp.net-identity-3

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 ')'.

Then i thought hmm, hey the info on Roles says navigational property, maybe i need to include?

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'.

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

Solution

  • This resolved itself, by porting the application from RC1 to RC2

    So I feel it's safe to say it was a bug in RC1.