I have two lists
user.Roles
repository.Roles
and RoleViewModel
public class RoleViewModel
{
public int RoleID { get; set; }
public bool InRole { get; set; }
public string Name { get; set; }
public RoleViewModel() { }
public RoleViewModel(Role role,bool inRole)
{
this.RoleID = role.RoleID;
this.Name = role.Name;
this.InRole = inRole;
}
}
I need to generate any collection like List<RoleViewModel> which will be a composition of RoleViewModel items with inRole field set to true. And all other possible roles which are placed in repository.Roles collection and will be converted to RoleViewModels.
Can I use linq to generate needed list?
Thank you
It seems like you want a view model for every role in the repository, and if the user is in that role, the
inRoleargument to be true. Please correct me if I’m wrong.To do that, you need a left join from the repository roles to the user’s roles:
This joins the set of roles in the repository to the set of roles for the user. It places the matching user roles into the
userRolessequence, then usesDefaultIfEmptyto return null foruserRoleif the user is not in the repository role. This way, ifuserRoleis not null, the user is in the repository role, but ifuserRoleis null, the user is not in the repository role.