我有如下实体模型类
public partial class User
{
public User()
{
this.webpages_Roles = new HashSet<webpages_Roles>();
}
public int UserID { get; set; }
public virtual ICollection<webpages_Roles> webpages_Roles { get; set; }
}
.
public partial class webpages_Roles
{
public webpages_Roles()
{
this.Users = new HashSet<User>();
this.Roles_X_ApplicationModules =
new HashSet<Roles_X_ApplicationModules>();
}
public int RoleId { get; set; }
public virtual ICollection<User> Users { get; set; }
public virtual ICollection<Roles_X_ApplicationModules>
Roles_X_ApplicationModules { get; set; }
}
.
public partial class Roles_X_ApplicationModules
{
public long ID { get; set; }
public Nullable<int> ModuleID { get; set; }
public Nullable<int> RoleID { get; set; }
public Nullable<bool> ViewPermission { get; set; }
public virtual ApplicationModule ApplicationModule { get; set; }
public virtual webpages_Roles webpages_Roles { get; set; }
}
。和
public partial class ApplicationModule
{
public ApplicationModule()
{
this.Roles_X_ApplicationModules =
new HashSet<Roles_X_ApplicationModules>();
}
public int ModuleID { get; set; }
public virtual ICollection<Roles_X_ApplicationModules>
Roles_X_ApplicationModules { get; set; }
}
您可以看到User
对象有一个导航属性,webpages_Roles
该属性又具有导航属性,Roles_X_ApplicationModules
然后又导航到ApplicationModule
..
现在我想从用户那里获取所有的 ApplicationModule ..如何使用导航属性编写查询..
我试过这样的东西..
var appModules = user.webpages_Roles.SingleOrDefault()
.Roles_X_ApplicationModules.Where(z => z.ViewPermission == true)
.Select(x => x.ApplicationModule);
但问题是,它不会向数据库发出单个查询。它拆分查询以获取webpages_Roles
atSingleOrDefault
然后另一个查询以Roles_X_ApplicationModules
基于获取 ,最后与匹配条件的RoleId
查询数量一样多以获取Roles_X_ApplicationModules
ApplicationModule
如何编写 LINQ 查询以便向数据库发出单个 sql 查询?