I have an enumeration declared as such
[Flags]
public enum PermissionEnum
{
[EnumStringValue("None")]
None = 0x0,
[EnumStringValue("Create")]
Create = 0x1,
[EnumStringValue("Edit")]
Edit = 0x2,
[EnumStringValue("View")]
View = 0x4
}
I subsequently have some linq that attempts to obtain the highest permission available.
List<SecurityRolePermissionView> AvailablePermissions = GetPermissions();
SecurityRolePermissionView HighestPermission = AvailablePermissions.OrderByDescending(o => o.Permission).FirstOrDefault();
With the current enumeration values this seems to do what it suggests it will do. However, I am pretty confident that this code is incorrect but I’m not sure I could explain why or how to achieve a correct implementation.
Either way, could someone please be kind enough to confirm if this is in-fact incorrect (and why) and provide an explanation of how to achieve my desired result?
EDIT: I might not have been clear enough in explaining what my highest permission is. This is a flags enumeration. Therefore it can contain any combination of None, Create, Edit, View. What I am looking to find as the highest is basically Create + Edit + View. If that Fails, Edit + View and lastly View
Enum ordering is the same as the ordering of the enum’s underlying type (C# specification section 7.9.5, “Enumeration operators”). Therefore, this is the order of your possible enum values:
If that ordering fits with your requirements, then your code is correct.