在下面的内容中,我想使用GetUsersRole,但是在显示的部分上遇到了困难。我想将Roles值与usersRole进行比较,如果Role == usersRole,则UserRole = true,否则为false。
基本上,我想得到这样的结果:
user1:是
user2:否
用户3:假
user4:是
取决于用户角色
类角色
public enum Role
{
User1 = 1,
User2= 2,
User3= 3,
User4= 4
}
我正在上课
private class UserRoleModel
{
public string Role { get; set; }
public bool UserRole { get; set; }
}
和方法
public Role[] UserRoles { get; set; }
private static UserRoleModel[] GetUsersRole(Role[]usersRole)
{
List<UserRoleModel> rolesList = new List<UserRoleModel>();
string[] Roles;
Roles = new string[] { Role.user1.ToString(), Role.User2.ToString(),
Role.user3.ToString(), Role.user4.ToString() };
foreach (var item in Roles)
{
rolesList.Add(new UserRoleModel
{
Role = item,
*UserRole = usersRole.Contains(item)* ////difficulty here
});
}
return rolesList.ToArray();
}
最佳答案
之所以遇到这个问题,是因为您实际上不需要字符串时将Role
值转换为字符串。将ToString()
移至实际需要的位置:
public Role[] UserRoles { get; set; }
private static UserRoleModel[] GetUsersRole(Role[]usersRole)
{
List<UserRoleModel> rolesList = new List<UserRoleModel>();
Role[] roles = (Role[]) Enum.GetValues(typeof(Role));
// or if you need the specific three values like in your example:
// Role[] roles = new Role[] { Role.User1, Role.User2, Role.User3, Role.User4 };
foreach (var role in roles)
{
rolesList.Add(new UserRoleModel
{
Role = role.ToString(),
UserRole = usersRole.Contains(role)
});
}
return rolesList.ToArray();
}