Linq to SQL,仅返回特定角色的用户

发布于 2024-08-16 12:02:19 字数 268 浏览 4 评论 0原文

我正在使用 linq to SQL 以及 asp.net 会员资格提供程序。我设置了三个角色,但只想获取特定角色的用户。这是我返回所有用户的查询

  public IQueryable<aspnet_User> GetAllMembers()
  {
     return db.aspnet_Users.OrderByDescending(u => u.UserName);
  }

有人能指出我正确的方向吗?

谢谢

I'm using linq to SQL along with asp.net membership provider. I have three roles set up but want to get only the users of a particular role. This is my query to return all users

  public IQueryable<aspnet_User> GetAllMembers()
  {
     return db.aspnet_Users.OrderByDescending(u => u.UserName);
  }

Can anyone point me in the right direction?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

百思不得你姐 2024-08-23 12:02:20

看来您需要讨论的内容 这里。 Tvanfosson 未经测试的答案得到了最多的支持:

var users = db.aspnet_Users
              .Where(u => u.Active)
              .Any(u => u.aspnet_UsersInRoles
                         .Any(r => r.aspnet_Roles.RoleName == "Auth Level 1" ));

根据提出问题的人(约翰)的说法,这对他来说不太有用,所以他最终这样做了:

var AuthUsers = from aspnet_User in db.aspnet_Users
                join userinroles in db.aspnet_UsersInRoles on aspnet_User.UserId equals userinroles.UserId
                where aspnet_User.Active 
                && userinroles.aspnet_Role.RoleName == "Auth Level 1"
                select aspnet_User;

希望这两个中的一个能够工作(请让我们知道哪一个适用于你)。

It looks like you need the same thing that was discussed here. Tvanfosson's untested answer was the most upvoted:

var users = db.aspnet_Users
              .Where(u => u.Active)
              .Any(u => u.aspnet_UsersInRoles
                         .Any(r => r.aspnet_Roles.RoleName == "Auth Level 1" ));

According to the person who asked the question (John) this didn't quite work for him so he ended up doing this instead:

var AuthUsers = from aspnet_User in db.aspnet_Users
                join userinroles in db.aspnet_UsersInRoles on aspnet_User.UserId equals userinroles.UserId
                where aspnet_User.Active 
                && userinroles.aspnet_Role.RoleName == "Auth Level 1"
                select aspnet_User;

Hopefully one of those two will work (please let us know which works for you).

半透明的墙 2024-08-23 12:02:20

为什么不使用会员资格提供程序附带的内置功能?

看看这里...

why not use the built in functions that come along with the membership provider?

Have a look here...

裂开嘴轻声笑有多痛 2024-08-23 12:02:20

另一种选择是分两步执行此操作:

  1. 首先通过查询 aspnet_UsersInRole 表查找您角色中的用户列表,
  2. 然后从 aspnet_Users 表中筛选出以下用户:在该列表中

类似这样的内容:

string roleName = "someRole";

// get the user id's in that role
var usersInRole = db.aspnet_UsersInRoles
                        .Where(ur => ur.RoleName == roleName)
                        .Select(ur => ur.UserId).ToList();

// then find the users that are in that list of users
var users = db.aspnet_Users.Where(u => usersInRole.Contains(u.UserId));

当然,在您的具体示例中,带有连接的直接方法之一将起作用 - 但有时这几乎难以表达,并且引入第二步可能最终会让事情变得更容易。您甚至可能希望根据其他一些标准进一步限制此用户列表,这可能无法通过联接轻松表达。

不管怎样,您一定能够找出这些用户! :-)

Another option is to do this operation in two steps:

  1. first find the list of users in your role by querying the aspnet_UsersInRole table
  2. then filter out the users from your aspnet_Users table that are in that list

Something like:

string roleName = "someRole";

// get the user id's in that role
var usersInRole = db.aspnet_UsersInRoles
                        .Where(ur => ur.RoleName == roleName)
                        .Select(ur => ur.UserId).ToList();

// then find the users that are in that list of users
var users = db.aspnet_Users.Where(u => usersInRole.Contains(u.UserId));

Of course, here in your concrete example, one of the direct approaches with a join will work - but sometimes that's just almost too tricky to express, and introducing a second step might make things easier in the end. You might even want to further limit down this list of users based on some other criteria, that might not be easily expressible with a join.

One way or another - you'll definitely be able to figure out those users! :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文