.net - How can I use linq to check if an flags/bitwise enumeration contains a type? -


the following lambda statemement returns null, when hoping return string value.

var countrycode = addresscomponents     .where(x => x.addresstype == addresstype.country)     .select(x => x.shortname)     .singleordefault(); 

now addresstype property of current instance i'm interrigating contains following data:

addresstype.political | addresstype.country

so it's containing 2 values.

of course, lambda not work, because value of country (lets assume it's 1) != value of political bitwise or country (lets assume it's 1 | 2 == 3).

any ideas?

i'm worried need have fraking ugly ...

((addresstypes & addresstype.country) == addresstype.country)

.. thoughts?

.net 4.0 has theenum.hasflagmethod:

x => x.addresstype.hasflag(addresstype.country) 

if not on .net 4.0, bitwiseandyou have there choice. if don't pattern, check out unconstrainedmelody, has extension method purpose. alternatively, can write 1 yourself; question of - anyone know workaround lack of enum generic constraint?


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -