I need to make a set of checkboxes where a user can select 1 or more markets. I would normally use a set of boolean values to accomplish this as it would be pretty strait forward and in my mind not take up much space in the database or take that long to load as they are only boolean values. It was suggested to me that I could use a single enum value in the database for the list of checkboxes. For example here is the enum
[Flags]
public enum Markets
{
Canada = 2,
USA = 4,
Iceland = 8,
Brazil = 16,
Italy = 32,
Japan= 64,
}
Then in the database I could store 14 for Canada Usa and Iceland. I was wondering is enums the way to go in MVC for checkboxes and if so how would it work?
Note i'm using C# MVC4 and entity framework 5 but i'm about to transition to MVC5 entity framework 6.
SELECT * FROM SomeTable T WHERE T.Markets & 16 = 16 AND T.Markets & 64 = 64
. This is not only ugly as hell, it is also inefficient (you cannot index theMarkets
column). Unless you are working with a really constrained environment, I would not follow that path. – rsenna Dec 5 '13 at 15:45BIT
dataype is optimized. See technet.microsoft.com/en-us/library/ms177603.aspx: "If there are 8 or less bit columns in a table, the columns are stored as 1 byte. If there are from 9 up to 16 bit columns, the columns are stored as 2 bytes, and so on.". And, of course, you could also follow the complete normalization path (create aMarkets
table, and add another table for the n * n relationship). – rsenna Dec 5 '13 at 15:50