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); } }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With