How to retrieve Users with a specific role via Lin

2019-05-09 23:09发布

问题:

I need to retrieve all users of a specific role, something I think should be made straightforward, however, here is the problem.

The Roles navigation Property of ApplicationUser is not the list of roles (IdentityRole) but a collection of (IdentityUserroles) the intermediate table that links users and roles. So, this is the code I have, but it does not work.

[HttpPost]
[Authorize(Roles = "Admin, Recepcionista, Orientador")]
public ActionResult SearchResults(SearchCriteriaViewModel criteria)
{


    List<SearchResultViewModel> results = new List<SearchResultViewModel>();
    var clients = new List<ApplicationUser>();

    using (var context = new ApplicationDbContext())
    {
        IdentityUserRole role = new IdentityUserRole();
        var tempRole = (from _role in context.Roles
                        where _role.Name == "client"
                        select _role).FirstOrDefault();
        role.Role = tempRole;

        clients = (from client in context.Users
                   where client.Email.Contains(criteria.SearchCriteria)
                   || client.FirstName.Contains(criteria.SearchCriteria)
                   || client.MiddleName.Contains(criteria.SearchCriteria)
                   || client.LastName.Contains(criteria.SearchCriteria)
                   || client.SecondLastName.Contains(criteria.SearchCriteria)
                   || client.UserName.Contains(criteria.SearchCriteria)
                   && client.Roles.Contains(role)
                   select client).ToList();


    }

    foreach (var client in clients)
    {
        results.Add(new SearchResultViewModel()
        {
            Email = client.Email,
            FirstName = client.FirstName,
            MiddleName = client.MiddleName,
            LastName = client.LastName,
            SecondLastName = client.SecondLastName,
            UserName = client.UserName
        });
    }

    SearchClientViewModel model = new SearchClientViewModel();
    model.SearchResults = results;

    return View("Index", model);

}

Linq throws the following Exception

An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code

Additional information: Unable to create a constant value of type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole'. Only primitive types or enumeration types are supported in this context.

How can this be done, Why the navigation properties is not a collection of Roles, could this be a bug we need to report?

回答1:

Error message is quite clear: you can't call Contains on collection of non-primitive objects.

Try following:

    clients = (from client in context.Users
               where client.Email.Contains(criteria.SearchCriteria)
               || client.FirstName.Contains(criteria.SearchCriteria)
               || client.MiddleName.Contains(criteria.SearchCriteria)
               || client.LastName.Contains(criteria.SearchCriteria)
               || client.SecondLastName.Contains(criteria.SearchCriteria)
               || client.UserName.Contains(criteria.SearchCriteria)
               && client.Roles.Select(r => r.Name).Contains("client")
               select client).ToList();


回答2:

I'm quite sure there is an API in Identity Framework to do so. But for your exception, please try :

&& client.Roles.Count(r => r.Name == "client") > 0