c# - Multiple Bitwise Flag Combos -
i have method takes enum flags parameter of items display. items in database can have multiple flags set, , value passed method can have multiple flags set. eg:
item1 = flag1 item2 = flag1 | flag3 item3 = flag2 | flag3
i want able pass these values method , have corresponding items returned.
- if pass flag2 | flag3, return item2 , item3 (because each flag matches 1 of flags set in entity)
- if pass flag1, return item1 , item2
...etc. i've been experimenting .where , .any , still have absolutely no clue how this, if it's possible. i'm targetting .net 4.5 , using ef5.
use bitwise and operator
myflags value = myflags.flag2 | myflags.flag3; myflags item1 = myflags.flag1; myflags item2 = myflags.flag1 | myflags.flag3; myflags item3 = myflags.flag2 | myflags.flag3; bool matchitem1 = (value & item1) > 0; //false bool matchitem2 = (value & item2) > 0; //true bool matchitem3 = (value & item3) > 0; //true [flags] enum myflags { flag1 = 1, flag2 = 2, flag3 = 4 }
Comments
Post a Comment