【发布时间】:2018-03-24 18:56:44
【问题描述】:
对于长期使用 LINQ lambda 表达式的用户来说可能很容易,但我只是在这里碰壁......
我正在尝试获取具有特定 ID 的用户可以访问的所有区域对象。如果出于任何原因很重要,我正在使用实体框架。
context.AspNetUserRoles
.Where(u => u.UserId == _currentUserId)
.Select(ur => ur.AspNetRoles.RolePagePermissions.Select(pp => pp.Page.Area))
.GroupBy(a => a.Select(g => g.Page.Select(gg => gg.Area)))
.ToList()
我得到的是List<IGrouping<IEnumerable<IEnumerable<Area>>>,而不是List<Area>
有人知道如何轻松做到这一点吗?
由于许多页面可以在同一个区域中,并且角色与页面相关联,因此以下代码会返回许多重复区域(因为一个用户可以访问许多页面,因此每个页面返回一个区域):
context.AspNetUserRoles.Where(u => u.UserId == _currentUserId).SelectMany(ur => ur.AspNetRoles.RolePagePermissions.Select(pp => pp.Page.Area)).ToList()
这是用于测试的模型:
public partial class Area
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Area()
{
this.Page = new HashSet<Page>();
}
public System.DateTime CreationDate { get; set; }
public int CreationUser { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public byte Activated { get; set; }
public string SimpleLineIcon { get; set; }
public string ControllerName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Page> Page { get; set; }
}
public partial class AspNetRoles
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public AspNetRoles()
{
this.AspNetUserRoles = new HashSet<AspNetUserRoles>();
this.RolePagePermissions = new HashSet<RolePagePermissions>();
}
public string Id { get; set; }
public string Name { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AspNetUserRoles> AspNetUserRoles { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<RolePagePermissions> RolePagePermissions { get; set; }
}
public partial class AspNetUserRoles
{
public int Id { get; set; }
public string UserId { get; set; }
public string RoleId { get; set; }
public virtual AspNetRoles AspNetRoles { get; set; }
public virtual AspNetUsers AspNetUsers { get; set; }
}
public partial class AspNetUsers
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public AspNetUsers()
{
this.AspNetUserClaims = new HashSet<AspNetUserClaims>();
this.AspNetUserLogins = new HashSet<AspNetUserLogins>();
this.AspNetUserRoles = new HashSet<AspNetUserRoles>();
}
public string Id { get; set; }
public string Email { get; set; }
public bool EmailConfirmed { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public Nullable<System.DateTime> LockoutEndDateUtc { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
public string UserName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AspNetUserClaims> AspNetUserClaims { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AspNetUserLogins> AspNetUserLogins { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AspNetUserRoles> AspNetUserRoles { get; set; }
}
public partial class Page
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Page()
{
this.RolePagePermissions = new HashSet<RolePagePermissions>();
}
public System.DateTime CreationDate { get; set; }
public int CreationUser { get; set; }
public int Id { get; set; }
public string Title { get; set; }
public byte Activated { get; set; }
public int AreaId { get; set; }
public string SimpleLineIcon { get; set; }
public string ActionName { get; set; }
public virtual Area Area { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<RolePagePermissions> RolePagePermissions { get; set; }
}
public partial class RolePagePermissions
{
public System.DateTime CreationDate { get; set; }
public int CreationUser { get; set; }
public int Id { get; set; }
public string RoleId { get; set; }
public int PageId { get; set; }
public byte AccessAllowed { get; set; }
public Nullable<System.DateTime> AccessAllowedByDate { get; set; }
public virtual AspNetRoles AspNetRoles { get; set; }
public virtual Page Page { get; set; }
}
【问题讨论】:
-
context.AspNetUserRoles.Where(u => u.UserId == _currentUserId).Select(ur => ur.AspNetRoles.RolePagePermissions.Select(pp => pp.Page.Area)).GroupBy(a => a.SelectMany(g => g.Page.Select(gg => gg.Area))).ToList() -
在第一次选择中使用
SelectMany。 -
@SoheilAlizadeh 得到了列表
>>,所以少了一个 IEnumerable。任何想法如何仅获取 List? -
你可以在你的问题中添加你的模型吗?我想测试一下。
-
在问题中添加了模型
标签: c# entity-framework linq lambda