I have a list of records that contains a flags enum value. I am trying to filter on one or more value based on the flags supplied. The description of the HasFlag method is as follows:
true if the bit field or bit fields that are set in flag are also set in the current instance; otherwise, false.
I would expect given an enum:
[Flags]
public enum MyFlaggedEnum
{
Unknown = 0,
First = 1,
Second = 2,
Third = 4,
Fourth = 8,
Fifth = 16,
Sixth = 32
}
And a record set containing a mixture of these values, assume that there are at least one record for each of the flags:
When I filter on one value:
var myFilteredList = MyMainList.Where(w => w.flagValue.HasFlag(MyFlaggedEnum.Third));
// myFilteredList returns the correct subset of records
When I filter on multiple values:
var myFilteredListMultiple = MyMainList.Where(w => w.flagValue.HasFlag(MyFlaggedEnum.First | MyFlaggedEnum.Fourth));
// myFilteredList returns an empty set
Am I misusing the HasFlag method; if so, how?
I am looking for a way to quickly filter the results so they can be displayed to the user in a datagridview based on filters they supply at runtime and can be changed at any time. I could loop through them or union multiple sets based on the flag filters requested by the user, but, was looking for a clean way.
EDIT: Whoever closed this question referring to this: How to check if any flags of a flag combination are set? does not understand the question. No, I am not adding First | Second values to my enum!!