I have this flag enum:
public enum DataAccessPoliceis
{
None = 0,
A = 1,
B = 2,
C = 4,
D = 8,
E = B | C | D, // 14
All = A | E // 15
}
I want to get int value (or list of int values for complex enum item) from int value:
int x = 9; // enum items => D | A
List<int> lstEnumValues = ???
// after this line ...
// lstEnumValues = { 1, 8 }
// and for x = 15
// lstEnumValues = { 1, 2, 4, 8, 14, 15 }
What's your solution for this question?
Answer of my question:
var lstEnumValues = new List<int>Enum.GetValues(typeof(DataAccessPoliceis)).Cast<int>())
.Where(enumValue => enumValue != 0 && (enumValue & x) == enumValue).ToList();
@dyatchenko and @Enigmativity thank you for your responses.