0

我有如下实体模型类

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_RolesatSingleOrDefault然后另一个查询以Roles_X_ApplicationModules基于获取 ,最后与匹配条件的RoleId查询数量一样多以获取Roles_X_ApplicationModulesApplicationModule

如何编写 LINQ 查询以便向数据库发出单个 sql 查询?

4

2 回答 2

0

尝试这个:

var appModules = from u in user
                 from w in u.webpages_Roles
                 from am in w.Roles_X_ApplicationModules
                 where am.ViewPermission == true
                 select am;

如果你想要预先加载,那么你只需要调用 ToList:

var appModules = (from u in user
                 from w in u.webpages_Roles
                 from am in w.Roles_X_ApplicationModules
                 where am.ViewPermission == true
                 select am).ToList();
于 2013-04-17T15:27:28.003 回答
0

您可以使用它Include()来执行此操作。例子:

card = Cards.Include(l => l.DocumentLinks)
            .Include(l => l.Charges.Select(ch => ch.DocumentLinks)
            .SingleOrDefault(c=>c.Id==id);

这适用于三个链接实体:

public class Card
{
    public Guid Id{get;set;}
    public virtual ICollection<DocumentLink> DocumentLinks{get;set;}
    public virtual ICollection<Charge> Charges{get;set;}
}

public class Charge
{
    ...
    public virtual ICollection<DocumentLink> DocumentLinks{get;set;}
}

public class DocumentLink
{
    ...
}
于 2013-04-17T10:36:06.840 回答