Search code examples
asp.net-mvclinqasp.net-membershiprole

MVC Linq selecting a list of users with the have role of


What is wrong with my query below? This is using the standard Membership tables.

var clientRole = rushDB.aspnet_Roles.Single(r => r.LoweredRoleName == "client" );
        //var users = rushDB.aspnet_Users.Where(u => u.aspnet_Roles.Contains(client)).AsEnumerable();

        var users = from u in rushDB.aspnet_Users
                        where u.aspnet_Roles.Contains(clientRole)
                    select u;

        return View(users.ToList());

I get this error on my view... Unable to create a constant value of type 'RushToIt.Models.aspnet_Roles'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.


Solution

  • I would expect that you are trying (or rather LINQ is trying) to use the clientRole as a where-clause to a select statement. You can only send simple types are parameters.

    Instead you would need to evaluate the role inline.

    However it's probably easier if you just traverse in reverse (that sounds good, right?). You should be able to do this:

    var clientRole = rushDB.aspnet_Roles.Single(r => r.LoweredRoleName == "client" );
    return View(clientRole.aspnet_Users.ToList());