I've never really written linq queries for this sorta thing as I would rather use the built in active directory repository. If you want to go this route:
I would start first by making sure that your database has the appropriate foreign keys configured:
Table 'Roles'
int Id (PK)
Table 'Users'
int Id (PK)
Table 'UsersAndRoles'
int RoleId (FK)
int UserId (FK)
If this is done correctly, Subsonic3 will generate the classes for you creating IQueryable child objects. If you want to get all roles with the user attached, you'll also need to add a list of UsersAndRoles to the Role Object. You'll have to name this differently as you can't name it the same as the IQueryable object:
public partial class Role()
{
public int Id { get; set; }
public string Rolename { get; set; }
public string Description { get; set; }
private IList<UsersAndRoles> _UserToRoleLinks
public IList<UsersAndRoles> UserToRoleLinks
{
get
{
if(_UserToRoleLinks == null)
_UserToRoleLinks = this.UsersAndRoles.ToList();
return _UserToRoleLinks;
}
}
}
public partial class UsersAndRoles
{
private Role _Role;
public Role Role
{
get
{
if (_Role == null)
_Role = this.Roles.SingleOrDefault();
return _Role;
}
set
{
_Role = value;
}
}
private User _User;
public User User
{
get
{
if (_User == null)
_User = this.Users.SingleOrDefault();
return _User;
}
set
{
_User = value;
}
}
}
Now to get all roles with the users attached, do this:
var roles = Role.All();
to access all users for all roles:
foreach (var role in roles)
{
foreach (var userToRoleLink in role.UserToRoleLinks)
{
var currentUser = userToRoleLink.User;
}
}
Of course this is freehand so there are bound to be compile errors. Let me know if this helps or not. Good luck!