vicky vicky - 3 months ago 19
C# Question

List of users that not yet in any role, mvc c#

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.

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();

Answer

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();
Comments