Difficulty filling the list when comparing arrays

In the future I want to use GetUsersRole, but with difficulties with the part shown. I want to compare Roles values ​​with userRole, and if Role == usersRole, then UserRole = true, else false.

Basically, I want this to be as a result:

user1: true

user2: false

user 3: false

user4: true

depending on usersRole

Class role

public enum Role
{                                            
    User1 = 1,
    User2= 2,
    User3= 3, 
    User4= 4
}

I have a class

private class UserRoleModel
{
    public string Role { get; set; }
    public bool UserRole { get; set; }
}

and method

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();
}
+2
source share
4 answers

You ran into this problem because you turn values Roleinto strings when you really don't need a string. Move ToString()to where you really need it:

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();
}
+1
source

Check last update for correct answer

, . , , , u - Calling Contains

userRole.Contains(item);

, item Roles. , userRole, . , :

private static UserRoleModel[] GetUsersRole(Role usersRole)

:

private static UserRoleModel[] GetUsersRole(Role[] usersRole)

Update

, . , Contains methofd , String, , Type Role.

, , Enum Enum Enum

, , - :

:

public Role GetRole(string rolestring)
{
     Role result; 
     foreach(string rolename in Enum.GetNames(typeof(Role)))
     {
       if(rolename == rolestring)
       {
           try 
           {
             result = (Role) Enum.Parse(typeof(Role), rolename); 
           }
           catch(ArgumentException e)
           {
                 //Most unlikely we ever enter this catch s we know for sure we have role
                 //Process if role not found
                 throw;
           }
       }
     }
     return result;
}

UserRole = usersRole.Contains(GetRole(item));

Update

, .. , Contains , , Contains for Type Arrays, Type List<T>

Exists, Predicate bool.

, :

//just to be sure correct value is captured everytime
string copy = item;
//Predicate is in System.Predicate<T>
Predicate<string> predicate = itemtocheck => {   
       itemtocheck == copy;
   };
UserRole = Array.Exists(Enum.GetNames(typeof(Role)), predicate);

</" > , , . , .

+1

, , , .

-, , , [Flags], , , , , .

, Roles, - [Enum.GetNames(enum)]. - , , google GetDescription [Description(...)] .

enum.HasFlag(MyEnum.ValueToCheckAgainst) ( .NET 4.0), , .

, @Shekhar_Pro , , , , UserRoleModel s. , .

And the last part, returning these checkboxes back to the value Role, you have to do it manually. Write a function that takes an array of values Roleand AND all together (after you make it a legal enumeration of flags). Then just return the result. If you want something more "elegant" than this, you probably just want to make this function an extension to everything that you think should output these values ​​(for example, your view model).

0
source
*UserRole = usersRole.Contains(item)* ////difficulty here

Change this to:

UserRole = false;
foreach(Role r in usersRole)
{
    if(r == item)
    {
        UserRole = true;
        break;
    }
}
0
source

Source: https://habr.com/ru/post/1787931/


All Articles