【问题标题】:Linq query from navigation properties来自导航属性的 Linq 查询
【发布时间】:2014-04-08 05:36:31
【问题描述】:

在实体框架中,我有两个表(两个实体):具有一对多关系的人员和角色。在人员表中,我有一个角色导航属性:

public virtual ICollection<Role> Roles { get; set; }

我能够检索到所有担任“酒保”角色的人:

var listPeople = (from p in SiContext.People
                  where p.Roles.Any(x => x.Name == "barman")
                  select p).ToList();

如何检索所有具有相关角色的人?我想要人名和姓氏以及角色名称(即在角色表中)

谢谢

【问题讨论】:

    标签: c# visual-studio-2010 linq entity-framework


    【解决方案1】:

    这个?

    var peoples = SiContext.People.Include("Roles");
    

    或

    var peoples = SiContext.People.Include(p => p.Roles);
    

    【讨论】:

      【解决方案2】:

      您可以从多个实体中进行选择:

      from p in SiContext.People
      from r in p.Roles
      select new { Name = p.Name, ..., Role = r.Name }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多