LINQ to Entities:查询示例



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

我需要一个列表,以便能够以这样的方式在病房后循环:

foreach (QASModel.UserGroup usergroup in ...)

如果你能提供一些关于如何:的例子,可以获得奖励积分

  • 根据用户ID获取用户的角色权限"路径"
  • 根据用户的ID获取用户的角色
  • 根据角色ID获取特定角色的所有用户这也会有所帮助

我认为这就是您想要的:

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
    }
}

请注意,我没有通过编译器来运行这个,但这应该会让你对你想要的东西有一个基本的了解。

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;

没有时间检查,所以如果有任何问题,请告诉我,我会尽力解决的。

相关内容

  • 没有找到相关文章

最新更新