Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public int GetUserRole(int UserId)
       {
           int adminRole = 0;
           using (VMSDBContext context = DAL.Instance.NewDefaultDbContext())
           {
               var roleId = (from objRole_mappings in context.Role_mappings.Where(objRole_mapping => objRole_mapping.User_id == UserId)
                             join objRoles in context.Roles on objRole_mappings.Role_id equals objRoles.ID
                             select
                                 objRoles.Is_admin).Any();
                                 adminRole = roleId == true ? 0 : 1;
                                 return adminRole;
           }
       }
Posted
Comments
Chivillaz 18-Sep-15 5:39am    
i am not able to retrieve a boolean value from database is this query is wrong or right?

1 solution

You could just do this
C#
bool roleIsAdmin = (from objRole_mappings in   context.Role_mappings.Where(objRole_mapping => objRole_mapping.User_id == UserId)
                            join objRoles in context.Roles on objRole_mappings.Role_id                         equals objRoles.ID
                            select
                                objRoles.Is_admin
                           ).FirstOrDefault();


This way your function will
C#
return roleIsAdmin == true ? 1 : 0;
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900