0

I have a Select List of all users from Identity that pulls in CompanyName from ApplicationUser.

<select asp-for="Contact.GeneralClientName" class="custom-select mr-sm-2" asp-items="@(new SelectList(Model.Users, "Id", "CompanyName"))">
    <option selected="selected" value="0">Please Select</option>
</select>

In this list I need to omit users who have 1 of 2 roles: ContactManagers and ContactAdministrators

Currently I'm querying the users using:

Users = await UserManager.Users.ToListAsync();

I thought about giving each new user a role and then filtering the List based on that, but at this point that would be a big redo. I only need to hide 2 users that each have one of those roles associated with them, any help is greatly appreciated.

Thanks,

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Oscar Arango
  • 113
  • 1
  • 1
  • 11

1 Answers1

1

You can try with filtering the users with specific roles

var result = await UserManager.Users
   .Include(user => user.Roles)
   .Where(user => user.Roles.Any(r => r.Name == "ContactManagers" || r.Name == "ContactAdministrators"))
   .ToListAsync();
  • your solution looks promising. I have implemented it but I now have another question. In order for me to use .Roles, do I need to generate the property or field? I get a red line under .Roles within the include clause. – Oscar Arango Sep 09 '20 at 17:52
  • Use [this](https://stackoverflow.com/questions/51004516/net-core-2-1-identity-get-all-users-with-their-associated-roles/51005445#51005445) approach if you use .NET Core – Stefan Jovanchevski Sep 09 '20 at 18:32