EF 多表联合查询

 

//方式1:操作远程数据
var user = GetAll().AsNoTracking()
.Include(u => u.AdminUserRoles)
.Include(u => u.AdminUserRoles.Select(ar => ar.Role))
.Include(u => u.AdminUserRoles.Select(ar => ar.Role).Select(r => r.RolePermissions))
.Include(u => u.AdminUserRoles.Select(ar => ar.Role).Select(r => r.RolePermissions.Select(rp => rp.Permission)))
.SingleOrDefault(u => u.Id == userId);
 
//方式2:操作远程数据
var user = GetAll().AsNoTracking()
.Include(nameof(AdminUser.AdminUserRoles))
.Include(nameof(AdminUser.AdminUserRoles) + "." + nameof(AdminUserRole.Role))
.Include(nameof(AdminUser.AdminUserRoles) + "." + nameof(AdminUserRole.Role) + "." + nameof(Role.RolePermissions))
.Include(nameof(AdminUser.AdminUserRoles) + "." + nameof(AdminUserRole.Role) + "." + nameof(Role.RolePermissions) + "." + nameof(RolePermission.Permission))
.SingleOrDefault(u => u.Id == userId);
 
 
//获取不到管理员信息
if (user == null)
{
    throw new ArgumentException("找不到id=" + userId + "的用户");
}
 
//操作内存数据
return user.AdminUserRoles
.Select(ar => ar.Role)
.SelectMany(r => r.RolePermissions)
.Select(rp => rp.Permission)
.Any(p => p.ControllerName.ToLower() == controllerName.ToLower()
&& p.ActionName.ToLower() == actionName.ToLower()
&& p.HttpMethod.ToLower().Contains(httpMethod.ToLower()));

 

 

posted @ 2018-07-25 10:01  linyongqin  阅读(1152)  评论(0编辑  收藏  举报