Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query for objects with certain enum flag on, using a db int to store it

I have an object with a Flag enum with several possible "uses". The flag enum uses the proper power of 2.

Checking if a variable has a certain flag on, I can do it using the .NET 4 HasFlag()

BUT:

If I store that flag combination as a int in database... how can I retrive the objects that have certain flag on using Entity Framework?

For example, if my object is a "Contact" type, I would like to query those of them that are actually "Customers and Friends", being Customers and Friends flags in the ContactType Enum.

like image 875
Romias Avatar asked Jan 18 '23 04:01

Romias


1 Answers

db.Contacts.Where(c => (c.Flag & MyEnum.Flag3) != 0).ToList();
like image 136
usr Avatar answered Feb 03 '23 21:02

usr