kez February 2016

Get users by user role and subsidiary ID

In ASP.NET Identity we have following tables

  • AspNetUsers
  • AspNetRoles
  • AspNetUserRoles

Additionally I added the following column to the AspNetUsers table

  • Subsidiary_ID

Now I want to load a list with following filters

  • RoleName == "Approver"
  • Subsidiary_ID == "04"

So for that purpose I'm trying to write query like below

List<AspNetUser> approverprofiles = db.AspNetUsers.Where(w => w.Subsidary_ID == "04").Where(w => w.Name == "Approver").ToList();

but then I'm getting this error

'AspNetUser' does not contain a definition for 'Name' and no extension method 'Name'

Answers


tmg February 2016

You are searching for roleName in AspNetUsers table, that does not exist. If you want to get the users that have Subsidiary_ID == "04" and they are in role with RoleName == "Approver" then you must query like this:

 List<AspNetUser> approverprofiles = db.AspNetUsers
    .Where(w => w.Subsidary_ID == "04" && w.Roles.Any(r => r.Name == "Approver"))
    .ToList();

Post Status

Asked in February 2016
Viewed 3,586 times
Voted 14
Answered 1 times

Search




Leave an answer