suppose i am checking user has any one of role or not. suppose all roles are stored in variable separated in comma
var roles ="'HR','Admin','Account'" etc
now how could i detect that a specific user has any of the above role by EF query ?
i want to use contains function where like to provide all roles name separated by comma.
suppose tell me how could i compose this sql by EF
select count(*) from UserRoles where userid=1 and rolename in (roles)
i know the above sql is not compile....it is just a pesudo sample which help some one to understand what kind of query i want to compose by EF.
i got one closer EF query but not sure does it work or not.
string userId = User.Identity.GetUserId();
ApplicationDbContext db = new ApplicationDbContext();
var role = (from r in db.Roles where r.Name.Contains("Admin") select r).FirstOrDefault();
var users = db.Users.Where(x => x.Roles.Select(y => y.RoleId).Contains(role.Id)).ToList();
if (users.Find(x => x.Id == userId) != null)
{
// User is in the Admin Role
}
else
{
//User is not in the Admin Role
}guide me how could i achieve this.