Search code examples
c#asp.net-mvcrolessimplemembership

List of users that not yet in any role, (MVC4 with SimpleMembership and entity framework 5)


I am using mvc 4 with entity framework. I need a list of all users that have not yet been assigned any role. I cannot find any predefined function in Role.

enter image description here

Output will be.

enter image description here

My current code is as following.

var users = db.UserProfileEFMs
    .ToList()
    .Where(o => !Roles.GetRolesForUser(o.UserName).ToList<string>().Any())
    .ToList();

but it is not working properly or is too slow. Is there any another way to get that kind of list.

I am looking for an Extentions method for Roles like.

public static class Extentions
{
public static List<string>/*UserName*/ UsersNotInAnyRole(this Roles /*System.Web.Security*/ roles)
{
    /*----code----*/
}
}

currently i am using the following code

var users = db.Database.SqlQuery<UserProfile>("select * from dbo.UserProfile where UserId not in(select UserId from dbo.webpages_UsersInRoles)").ToList();

Solution

  • I don't get better solution. I think following is the best one.

    var users = db.Database.SqlQuery<UserProfile>("select * from dbo.UserProfile where UserId not in(select UserId from dbo.webpages_UsersInRoles)").ToList();