Michael Seery

Michael Seery

  • NA
  • 85
  • 10.4k

Filter joined string array

Mar 2 2020 4:25 AM
I have a linq query that returns all users with their identity roles.
I am able to filter the results by using where claused however, I am unable to figure out how to filter based on a role name.
 
Below is my linq query.
var userList = (from user in _context.Users orderby user.UserName
                        select new UserForDetailDto
                              {
                                 Id = user.Id,
                                 UserName = user.UserName,
                                 Email = user.Email,
                                 Roles = (from userRole in user.UserRoles
                                                join role in _context.Roles
                                                on userRole.RoleId
                                                equals role.Id
                                                   select role.Name).ToArray()
                              }).AsQueryable();
 
The below is sample of what is returned.
[
   {
      "id": 6,
      "userName": "Adam",
      "email": "[email protected]",
      "roles": [
            "Supplier",
            "Assistant Buyer"
      ]
   },
   {
      "id": 1,
      "userName": "Admin",
      "email": "[email protected]",
      "roles": [
            "Administrator"
      ]
   }
]
 
 
I am able to filter the userList using a where clause such as the one below.
 
userList = userList.Where(u => u.UserName.ToLower().Contains(userParams.UserName.ToLower())); 
 
I would like to be able to add a where clause to the role.Name string array.
Where roles contains 'Supplier' for example.
 
Any help would be appreciated. 
 

Answers (7)