23

I have the following enumeration of membership roles:

public enum RoleName
{
    RegisteredUser,
    Moderator,
    Administrator,
    Owner
}

I want to be able to fetch all roles greater than or equal to a given role.

For instance I input Administrator and I get an IEnumerable with RoleName.Administration and RoleName.Owner

Something of this sort:

public static void AddUserToRole(string username, RoleName level)
{
    var roles = Enum.GetValues(typeof(RoleName)).Cast<R>().ToList().Where(role => level > role);

    foreach (var role in roles)
    {
        Roles.AddUserToRole(username, role);
    }
}
bevacqua
  • 47,502
  • 56
  • 171
  • 285
  • 1
    considering that enum as default is of INT type, isn't enough to compare values against filter int? – Tigran Jul 24 '11 at 15:53

7 Answers7

24

Probably depending on version of .NET. But this works very well for me:

There is no need to convert or to use special tricks. Just compare with the usual operators:

using System;

enum Test { a1, a2, a3, a4 }

class Program
{
    static void Main(string[] args)
    {
        Test a = Test.a2;

        Console.WriteLine((a > Test.a1));
        Console.WriteLine((a > Test.a2));
        Console.WriteLine((a > Test.a3));
        Console.WriteLine((a > Test.a4));

        Console.ReadKey();
    }
}

Output:

True
False
False
False
Ramūnas
  • 1,494
  • 18
  • 37
Bitterblue
  • 13,162
  • 17
  • 86
  • 124
20

You can use the following helper method to give you a set of roles allowed for a given role:

    private IEnumerable<RoleName> AllAllowedRoles(RoleName level)
    {
        return Enum.GetValues(typeof(RoleName)).Cast<RoleName>().Where(role => level >= role);
    } 

And then assign all of them to the user.

nakhli
  • 4,009
  • 5
  • 38
  • 61
7

in order to have a better definition of which role is greater than the other you need to assign numeric values to your roles like this:

public enum RoleName
{
    RegisteredUser = 2,
    Moderator = 4,
    Administrator = 8,
    Owner = 16
}

Now if you cast any instance of type RoleName to (int) you will get the numeric value and therefore you will be able to compare them against each other.

Note:
1. Here I use powers of 2 as values to allow combining RoleNames using bit-wise operators.

Mo Valipour
  • 13,286
  • 12
  • 61
  • 87
  • 5
    Wrong; you don't need to assign any values. – SLaks Jul 24 '11 at 15:56
  • 1
    true that but as I said: in order to have a "better definition". otherwise you can easily get confused. but by giving them values you can always quickly judge that. – Mo Valipour Jul 24 '11 at 15:58
  • 1
    Not sure using flags here is a good idea. most of the combinations are meaningless eg. Owner | RegisteredUser. – nakhli Jul 24 '11 at 16:02
2

went with this:

    public static void AddUserToRole(string username, RoleName level)
    {
        var roles = RolesLesserThanOrEqualTo(level);

        foreach (var role in roles)
        {
            Roles.AddUserToRole(username, role.Name());
        }
    }

    public static IEnumerable<RoleName> RolesLesserThanOrEqualTo(RoleName level)
    {
        return Enum.GetValues(typeof(RoleName)).Cast<RoleName>().Where(role => level >= role);
    }
bevacqua
  • 47,502
  • 56
  • 171
  • 285
0

Your code is exactly correct, except that you need to Cast<int>() and you don't need ToList().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0
public enum RoleName : int
{
    RegisteredUser = 7,
    Moderator = 8,
    Administrator = 9,
    Owner = 10
}

 public List<RoleName> GetGreaterOrEqual(RoleName role)
    {
        List<RoleName> list = new List<RoleName>();
        foreach (RoleName rn in Enum.GetValues(typeof(RoleName)))
        {
            if(rn >= role)
                list.Add(rn);
        }
        return list;
    }

You could check this out Can you loop through all enum values?

Community
  • 1
  • 1
psu
  • 379
  • 1
  • 5
  • 15
0

You have to take into consideration the underlying type of the enum. Thus:

static IEnumerable<T> Get<T>(T value) where T : struct {
    Type underlyingType = Enum.GetUnderlyingType(typeof(T));
    var compareToMethod = underlyingType.GetMethod(
                              "CompareTo",
                              new[] { underlyingType }
                          );
    var values = Enum.GetValues(typeof(T));
    for (int index = 0; index < values.Length; index++) {
        int comparison = (int)compareToMethod.Invoke(
            Convert.ChangeType(value, underlyingType),
            new object[] { values.GetValue(index) }
        );
        if (comparison >= 0) {
            yield return (T)values.GetValue(index);
        }
     }
 }

I didn't feel like trying to come up with a good name, but this works:

var largerValues = Get<RoleName>(RoleName.Moderator);
foreach(var largerValue in largerValues) {
    Console.WriteLine(largerValue);
}
jason
  • 236,483
  • 35
  • 423
  • 525