LINQ(to SQL)可以进行按位查询吗?
我有一个用户表,其中包含用户所属角色的位掩码。 我想选择属于位掩码值中的一个或多个角色的用户。 例如:
select * from [User] where UserRolesBitmask | 22 = 22
这将选择位掩码中具有角色“2”、“4”或“16”的所有用户。 这可以用 LINQ 查询来表达吗? 谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
作为我的谷歌同事的旁注:
UserRolesBitmask | 用户角色位掩码 22 == 22 选择所有没有任何其他标志的用户(它不是过滤器,就像说
1==1
)。您想要的是:
UserRolesBitmask & 22 == 22
选择在其位掩码中具有所有角色的用户,或者:UserRolesBitmask & 22 != 0
选择位掩码中至少具有其中一个角色的用户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 saying1==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 bitmask我认为这会起作用,但我还没有测试过。替换您的 DataContext 对象的名称。 YMMV。
I think this will work, but I haven't tested it.Substitute the name of your DataContext object. YMMV.
如果这不起作用,您可以随时使用 ExecuteCommand:
If that doesn't work you could always use
ExecuteCommand
: