within a form I have two listbox controls these controls are used for mapping users to a role. Within the listbox on the left(available users) I need to show a list of all users associated to the current department. The listbox on the right displays a list of users from the department that are already mapped to the role.
Each listbox is mapped to it’s own collection. What I would like to do is to filter out the names that may appear in the mapped users collection from the available users collection when the listbox initially loads. In looking at the illustration the listbox on the right should only display 1 available user for mapping to this role (In the illustration it would be alguser4)
My inital thought was to create a new collection (avaialbleuserscoll) and attempt to filter using linq. Where I am stuck is a user may be mapped to multiple roles therefore I can’t take the current approach and just test for a count = 0. What I need to do is to test to ensure that the count is == 0 or that the role id does not equal the current roleid.
I’d appreciate any guidance on how to get only users that are not mapped to the current role to display in the listAllUsers listbox
private void EditMappedUserDetails(int agencyid, int roleid)
{
List<SecurityUser> allUserColl;
List<SecurityUser> mappedUserColl;
List<SecurityUser> availableUserColl;
allUserColl = AdminFactory.GetUsersAndRoles(agencyid).OrderByDescending(n => n.UserName).ToList();
mappedUserColl = SelectedRoleData.FirstOrDefault().SecurityUserRoles.Select(r => r.SecurityUser).OrderByDescending(n => n.UserName).ToList();
availableUserColl = allUserColl.Except(mappedUserColl).ToList();
listAllUsers.DataTextField = "UserName";
listAllUsers.DataValueField = "SecurityUserId";
listMappedUsers.DataTextField = "UserName";
listMappedUsers.DataValueField = "SecurityUserId";
listAllUsers.DataSource = availableUserColl;
listMappedUsers.DataSource = mappedUserColl;
listAllUsers.DataBind();
listMappedUsers.DataBind();
editUserUpdatePanel.Update();
}
you can use the except function in linq to remove duplicates here is an example