With this classes:
class User
{
int UserId{get;set;}
string UserName{get;set;}
}
class Role
{
int RoleId{get;set;}
string RoleName{get;set;}
}
class User_Role
{
int UserId{get;set;}
int RoleId{get;set;}
}
I need to show in one ListBox the Roles that are Availables and in other ListBox the Roles that the User already have and can not be repeated. I did this in the code behind:
//Initialize the ObjectContext
MyDbContext ctx = new MyDbContext();
int userId; //some value pass by url;
var listRolesIds = (ctx.Roles.Select(r => r.RoleId )).ToList();
var listRolesIdsAsigned = ctx.User_Role.Where(u => u.UserId == userId).Select(u => new {u.UserId}).ToList();
var listRolesIdsAvailables = listRoles.Except(listRolesAsigned);
//once i have the ids, code for Bind the ListBox with the availables roles
It works but its dificult to maintain and I like to make it with an EntityDataSource but I dont know how to do it. Please if anyone can help me I would be grateful, thanks.