LINQ(to SQL)可以进行按位查询吗?

发布于 2024-07-06 19:00:24 字数 196 浏览 7 评论 0原文

我有一个用户表,其中包含用户所属角色的位掩码。 我想选择属于位掩码值中的一个或多个角色的用户。 例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

極樂鬼 2024-07-13 19:00:24

作为我的谷歌同事的旁注:
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 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 bitmask
肩上的翅膀 2024-07-13 19:00:24

我认为这会起作用,但我还没有测试过。替换您的 DataContext 对象的名称。 YMMV。

from u in DataContext.Users
where UserRolesBitmask | 22 == 22
select u

I 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
醉梦枕江山 2024-07-13 19:00:24

如果这不起作用,您可以随时使用 ExecuteCommand:

DataContext.ExecuteCommand("select * from [User] where UserRolesBitmask | {0} = {0}", 22);

If that doesn't work you could always use ExecuteCommand:

DataContext.ExecuteCommand("select * from [User] where UserRolesBitmask | {0} = {0}", 22);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文