1

I am using .Net Core 2.1 and I want to exclude the users that have the role "Admin" and "SimpleUser" I had followed this example Link

How i can add the where part? So far i have tried this with no luck:

 ViewData["ApplicationUserId"] = new SelectList( _context.Users.Include(u=>u.UserRoles).ThenInclude(u=>u.Role).Where(o=>o.UserRoles!="Admin"), "Id", "Company", products.ApplicationUserId);

enter image description here

ApplicationUserRole Class:

 public class ApplicationUserRole : IdentityUserRole<string>
    {
        public virtual ApplicationUser User { get; set; }
        public virtual ApplicationRole Role { get; set; }
    }
marios
  • 153
  • 1
  • 14

2 Answers2

0

The message in the attached image suggests that u.UserRoles is not a string, try something like u.UserRoles.All(r=>r.RoleId!="Admin" && r.RoleId!="SimpleUser") in your Where

Muaz Othman
  • 427
  • 4
  • 13
0

You will need to check that none of the roles are of "Admin" or "SimpleUser"

ViewData["ApplicationUserId"] = new SelectList( _context.Users.Include(u=>u.UserRoles).ThenInclude(u=>u.Role).Where(o => !o.UserRoles.Any(r => (r.Role == "Admin") || (r.Role == "SimpleUser"))), "Id", "Company", products.ApplicationUserId);
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175