I have a table of Users that includes a bitmask of roles that the user belongs to. I'd like to select users that belong to one or more of the roles in a bitmask value. For example:
select * from [User] where UserRolesBitmask | 22 = 22
This selects all users that have the roles '2', '4' or '16' in their bitmask. Is this possible to express this in a LINQ query? Thanks.
LINQ to SQL was the first object-relational mapping technology released by Microsoft. It works well in basic scenarios and continues to be supported in Visual Studio, but it's no longer under active development.
LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.
LINQ offers common syntax for querying any type of data source; for example, you can query an XML document in the same way as you query a SQL database, an ADO.NET dataset, an in-memory collection, or any other remote or local data source that you have chosen to connect to and access by using LINQ.
LINQ to Entities queries are comprised of LINQ standard query operators (such as Select, Where, and GroupBy) and expressions (x > 10, Contact. LastName, and so on). LINQ operators are not defined by a class, but rather are methods on a class.
As a side note though for my fellow googlers:
UserRolesBitmask | 22 == 22
selects all users that don't have any other flags (its not a filter, its like saying 1==1
).
What you want is either:
UserRolesBitmask & 22 == 22
which selects users which have all the roles in their bitmask or:UserRolesBitmask & 22 != 0
which selects users which have at least one of the roles in their bitmaskI think this will work, but I haven't tested it.Substitute the name of your DataContext object. YMMV.
from u in DataContext.Users
where UserRolesBitmask | 22 == 22
select u
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With