How to Use Enum Flags with EF 6.1.3? - c#

I think my query is not working because of the enum flag I have.
[Flags]
public enum Permissions
{
None = 0,
Read = 1,
Write = 2,
Delete = 4,
Full = 8
}
dbContext.UserStorages.FirstOrDefault(x => x.Permission.HasFlag(Permissions.Write));
In my sql database the column is an "int" column. and right now has a value of "8" if I change it to Permissions.Full then I will get the record back.

Permissions.Full shouldn't be 8 but 7 if it means Read + Write + Delete. In binary
Read -> %001
Write -> %010
Delete -> %100
Full -> Read | Write | Delete -> %001 | %010 | %100 -> %111 -> 7
In your case you are asking whether the second bit (i.e. %0010) is set in %1000 which obviously it isn't.

Related

Parsing multiple enum values (Flagged): Reading in a filter type from a query string

I'm plan to make a page that displays info about user's in the form of a table. Each column will be a property of the table. I want someone viewing the table to be able to choose ways to filter the users. There will be two check boxes: 1 for reported users and 1 for creation date. I'm planning to make an enum called UserFilter like so:
public enum UserFilter
{
None = 0,
Date = 1,
Reported = 2
}
If I need to add another type it's value will be set to 4 so that I can tell which enums are selected through a bitwise or (3 would be both 1 and 2). The problem I'm having is with reading in an enum from the query string. I guess I could do something like posting back with an int (0 for none, 1 for date, 2 for report, 3 for both) but I would like to try posting back with the actual string. I'm not sure how I would parse "Date|Reported" into an enum value that doesn't exist in UserFilter.
In a nutshell: Is there a clean way to parse "Date|Reported" into the value 3 without adding another value to my enum?
You could try something like
string enumString = "Date|Reported";
UserFilter uf = enumString.Split('|').ToList().Select(e =>
{
UserFilter u;
Enum.TryParse(e, true, out u);
return u;
}).Aggregate((u, c) => u = u | c);
I would however recomend that you change your enum to
public enum UserFilter
{
None = 1,
Date = 2,
Reported = 4
}
as if you have it your way None|Date is the same as Date, because 0 + 1 => 1
EDIT
As #ScottSelby stated, this would also work
UserFilter u = (UserFilter) 3;
//or 6 rather than 3 if you take my comment above into consideration

Using Enum Flags How to?

Currently I have 4 permissions in my website
Edit
Delete
Add
View
Everyone gets view permissions when someone subscribes to someone else.
Currently in I store each as a column in my database as a bit value.
I am thinking maybe I should use enum with flags but I have some questions.
The user will be able to choose the users permissions(eveyone gets view you cannot take that away). Right now it is easy as I use a checkbox and then save the bool value to the db.
My questions is what is the best way to convert checkboxes to the enum type? So if they choose add permission I can map that to the right enum type.
Wondering if there is a better way then
PermissionTypes t;
if(add == true)
{
t = PermissionTypes.Add
}
if(edit == true)
{
t += PermissionTypes.Edit // not sure if you even can do this.
}
I know with enum flags you can use the pipe and do something like this
PermissionTypes t = PermissionTypes.Add | PermissionTypes.Edit
I am wondering is it good practice to have another type in my enum like
AddEdit = Add | Edit
I am thinking that can result in many combinations(imagine if I had like 10 permissions or something like that).
Enums are simply integers values. By settings your enum values to different power of 2, you can easily have any combination you need:
enum ePermissions
{
None = 0,
View = 1,
Edit = 2,
Delete = 4,
Add = 8,
All = 15
}
ePermissions t;
t = (ePermissions)((add?ePermissions.Add:ePermissions.None) | (delete?ePermissions.Delete:ePermissions.None) | ...);
if you want to know if some user has the Add permission just do as follow:
canAdd = (currentPermission & ePermissions.Add) != 0;
On converting set of bool values to enum - for small set if would be ok. You can also use ?: operator to do the same in smaller number of lines:
[Flags]
enum Permissions
{
None = 0,
View = 1,
Edit = 2,
Delete = 4,
Add = 8,
}
var permissions =
(add ? Permissions.Add : Permissions.None) |
(edit ? Permissions.Edit : Permissions.None) |
(delete ? Permissions.Delete : Permissions.None);
If you have large number of values some sort of mapping of Control to Enum may be useful.
On AddEdit - this particular on is probably not useful. You can as easily write Add | Edit. Some menaingful combinations of permissions (or falgs) may be named specifically if code will use such combination in meanigfull way. I.e. CanModify = Add | View.

Is it ok to add an "All" item to a Flags enum?

If I have an enum with multiple values which can be present at the same time, I create a Flags enum:
[Flags]
public enum Foo
{
None = 0,
A = 1,
B = 2,
C = 4,
D = 8
}
If I now want to pass the fact that every value is set I would have to do something like this:
Bar bar = new Bar(Foo.A | Foo.B | Foo.C | Foo.D);
Would it be considered bad practice/harmful/blasphemy to add an additional element All?
All = 15
This would save some space and time when there are a lot of values and passing them all is a common scenario.
As far as I understand the mechanics of flags enums (=bitfields), this should work. Are there any side effects I am missing or other reasons why you should not do that?
You can do that. But maybe you shoudn't use the value 15, but
All = A | B | C | D
There is a dangerous gotcha here where if you change All but the calling components are not recompiled, they will send the old All. If this is fine with you than so be it.
Using bitwise operations is much easier to read
[Flags]
public enum MyEnum
{
None = 0,
First = 1 << 0,
Second = 1 << 1,
Third = 1 << 2,
Fourth = 1 << 3,
All = ~(-1 << 4)
}
The most likely place this will go wrong is
[Flags]
public enum Foo
{
None = 0,
A = 1,
B = 2,
C = 4,
D = 8
E = 16
All = 15
}
Us propeller heads tend to be microfocused, added E job done. oops, oh dear, damn....
And then you take the All idea one step further and feel a need for AllbutC.

Extending enum with Flags, interfere the current use

The system have a plain enum like this,
public enum SomeEnum : short
{
Ok = 0,
Missing = 17,
};
This enum are now into a situation where I need to mask some more information into it without change the appearence of the existing enum values. The enum will got some new values,
[Flags]
public enum SomeEnum : short
{
Ok = 0,
Missing = 17,
Blocking = 18, // Values could be anything
Low = 19, // Values could be anything
};
I was afraid there could be problem to the current enum usage. It appears that I'm right, but I hope i'm proved wrong with your help. The usage until today are built around SomeEnum.Ok. Also tomorrow, but the Ok need additional info. I need to mask the enum values without affect it's current behavior, which could came from any common reference;
someEnumVariable.ToString()
(int)someEnumVariable
someVar = SomeEnum.Ok
Enum.Parse(typeOf(SomeEnum), someString)
If I flag the enum with
var test = (SomeEnum.Ok | SomeEnum.Blocking);
Both flags can be founded i.e. test.HasFlags(SomeEnum.Ok) or test.HasFlags(SomeEnum.Blocking) but the enum represents as SomeEnum.Blocking, which aren't possible.
Any ideas?
Because the value of SomeEnum.OK is 0, calling HasFlag(SomeEnum.OK) will always return true. When you mask enums together, the process relies on the fact that the sum of any combination of enum values will be unique. Typically you would set these up starting using values of 2^0, 2^1, 2^2, etc. For example:
[Flags]
public enum SomeEnum : short
{
Ok = 1,
Missing = 2,
Blocking = 4, // Values could be anything
Low = 8, // Values could be anything
}
If you want to mask the values together, you'll have to use the Flags attribute. If you can't refactor and change the value of SomeEnum.OK, then you may have to rely on passing in a SomeEnum[], rather than a single masked SomeEnum value.
EDIT
Just a quick note on masking the values together using the enum defined above. Take the following code:
var t1 = (int)(SomeEnum.OK | SomeEnum.Missing); //t1 = 1 + 2 = 3
var t2 = (int)(SomeEnum.Missing | SomeEnum.Blocking); //t2 = 2 + 4 = 6
var t3 = (int)(SomeEnum.OK | SomeEnum.Low); //t3 = 1 + 8 = 9
var t4 = (int)SomeEnum.OK; //t4 = 1
var s1 = (SomeEnum.Ok).ToString(); //s1 = "Ok"
var s2 = (SomeEnum.Ok | SomeEnum.Missing).ToString(); //s2 = "Ok, Missing"
When these items are OR'ed together, .NET just adds the values together to produce a new, unique value that represents the combination of the OR'ed items. Using enum values that are powers of 2 ensures that the combinations will always be unique.
http://msdn.microsoft.com/en-us/library/system.flagsattribute.aspx
I think a quick introduction to binary OR is in order.
| doesn't just let you sneak two integers into one variable. It performs a mathematical operation.
Therefore, any value other than 0 OR'ed with 0 will return that value itself.
You will probably encounter further unexpected behavior if you supply flag values that are not a single bit, such as 1, 2, 4, 8, 16.
EDIT
If you want to build a Flags enum around the existing values, you can do that. Don't use the currently reserved bits 10001 (17) for any other value, and be careful when testing for Ok.
Here's an example of how to accomplish this using a bitflag. This will however likely force you to change your existing flag comparisons.
[FlagsAttribute]
enum tester
{
alpha = 1,
beta = 2,
gamma = 4,
reggy=3
}
class Program
{
static void Main(string[] args)
{
tester t = tester.alpha | tester.beta;
if (t == tester.alpha)
Console.WriteLine("alpha only");
if ((t & tester.alpha) != 0)
Console.WriteLine("alpha");
if ((t & tester.beta) != 0)
Console.WriteLine("beta");
if ((t & tester.gamma) != 0)
Console.WriteLine("gamma");
if (t == (tester.beta | tester.alpha))
Console.WriteLine("alphabeta");
//this will produce alpha, beta, alphabeta

C# Flag Charlie-Fox

I've been trying to emulate in C# a behavior which I was implementing without any problems in C++. I have a flag collection, which based on powers of 2, and a flag which I need to check if it exists in the collection.
byte flagCollection = 1 | 2 | 4 | 8;
byte flag = 2;
if ((flagCollection & flag) != 0) MessageBox.Show("y"); else MessageBox.Show("n");
Now, the problem is that whatever I set Flag to (2, 3, 100, 211), I always get "y", except for 0. Of course, 1, 2, 4, 8 would be some constants which have various meanings in my application.
In C++:
if (flagCollection & flag)
Would result in TRUE for the variables above.
The way you are doing it seems correct. I'm not really sure what behavior you are expecting. Any of the flags you listed (2, 3, 100, 211) do contain an element of 1 | 2 | 4 | 8. If you want to check whether all of them are present you should be doing
if ((flagCollection & flag) == flag)
// ...
well, if you're flag collection has OR'd 1,2,4,8, uh yes I think every positive integer that's not a multiple of 16 when &'d to it is going to return something. Or am I missing something?
Would this work?
[Flag]
public enum flagCollection {
Type1,
Type2,
Type4,
Type8,
}
flagCollection testValue = flagCollection.Type2
if ((testValue & flagCollection.Type2) == flagCollection.Type2) {
MessageBox.Show("y");
} else {
MessageBox.Show("n");
}
Not in front of the compiler so cant chek if that will work or not.

Categories