0

有人可以告诉我如何从这个模型中获取 LINQ to Entities 中用户的用户组关系吗?

我需要一个列表才能在病房后以这种方式循环它:

foreach (QASModel.UserGroup usergroup in ...)

如果您可以提供一些有关如何:

  • 根据用户 ID 获取用户的角色权限“路径”
  • 根据用户 ID 获取用户角色
  • 根据也有帮助的角色 ID 获取特定角色的所有用户。
4

2 回答 2

1

我认为这就是你要找的东西:

int uID = 55;    //The user ID that you're looking for

//Assumption for the data context name
QASDataContext ctx = new QASDataContext();   

//--(1)--
//Get the user's user groups
var user = (from u in ctx.Users
 where u.ID == uID
 select u).FirstOrDefault();

if(user != null)
{
    foreach(UserGroup in user.UserGroups)
    {
        //do your thing
    }

    //--(2)--
    //Get User's Role Permission Path(s)
    List<string> PermissionPaths = new List<string>();

    foreach(UserRole role in user.UserRoles)
    {
        foreach(UserRolesPer perPath in role.UserRolesPers)   //Can't see the whole table name
        {
            PermissionPaths.Add(perPath.Path);
        }
    }

    //You can use the PermissionPaths object now

    //--(3)--
    //Get a User's Role based on the User ID 
    //Use the same user object from above
    UserRole uRole = user.UserRole;
}

//--(4)--
//Get a all user(s) of a specific role based on the role ID
int myRoleID = 43;

var role = (from r in ctx.UserRoles
    where r.ID == myRoleID 
    select r).FirstOrDefault();

if(role != null)
{
    foreach(User u in role.Users)
    {
        // do something
    }
}

请注意,我没有通过编译器运行此程序,但这应该可以让您大致了解您正在寻找什么。

于 2011-05-29T21:39:10.413 回答
0
IEnumerable<UserGroup> userGroups = from ug in context.UserGroups
                                    from u in ug.Users
                                    where u.ID == [userID]
                                    select ug;

IEnumerable<string> userRoles = from ur in context.UserRoles
                                from rps in ur.UserRolesPermissions
                                where ur.UserId == id
                                select rps.Path;

IEnumerable<User> usersFromSpecificRole = from u in context.Users
                                          where u.UserRole.ID == [roleID]
                                          select u;

没有时间检查,所以如果有任何问题,请告诉我,我会尝试修复它们。

于 2011-05-29T22:09:44.660 回答