Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a list of users and their roles

I'm using the membership provider asp.net mvc 4. I'd like to get a list of users and their roles without Roles.GetRolesForUser() for each user.

In my application, the business requirements state that a user will only ever be assigned one role.

What I'm currently doing:

    [GridAction]
    public ActionResult _GetUsers()
    {
        var users = Membership.GetAllUsers().Cast<MembershipUser>().Select(n => new AdminAccountEditModel
        {
            Role = Roles.GetRolesForUser(n.UserName).FirstOrDefault(),
            IsApproved = n.IsApproved,
            Email = n.Email,
            UserName = n.UserName
        }).ToList();

        return View(new GridModel(users));
    }

Very inefficient. How do I fix this?

Thanks.

like image 204
Mike Avatar asked Nov 13 '22 13:11

Mike


1 Answers

In the past I've cheated somewhat when using the standard membership provider and have written a lot of complex queries directly against the tables in sql. What you're looking for is a simple join.

like image 180
spaceman Avatar answered Nov 15 '22 08:11

spaceman