具有连接表的Lambda表达式

时间:2016-02-07 17:34:00

标签: c# entity-framework lambda

我有以下实体

  1. 项目
  2. 组件
  3. ProjectComponent
  4. 我想写一个类似

    的方法
    GetProjectsByComponentId(int componentId)
    { /* some code*/}
    

    请帮我写一个lambda表达式。

    EF生成架构的片段:

    public partial class ProjectComponent
    {
        public int ID { get; set; }
        public Nullable<int> ProjectID { get; set; }
        public Nullable<int> ComponentID { get; set; }
        public string Comment { get; set; }
    
        public virtual Component Component { get; set; }
        public virtual Project Project { get; set; }
    }
    
    public partial class Project
    {
        public Project()
        {
            this.ProjectComponents = new HashSet<ProjectComponent>();
        }
    
        public int ID { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<ProjectComponent> ProjectComponents { get; set; }
    }
    
    public partial class Component
    {
        public Component()
        {
            this.ProjectComponents = new HashSet<ProjectComponent>();
        }
    
        public int ID { get; set; }
        public string Description { get; set; }
    
        public virtual ICollection<ProjectComponent> ProjectComponents { get; set; }
    }
    

1 个答案:

答案 0 :(得分:1)

ComponentID很遗憾,您提供的信息非常少,但我会尽力提供帮助。

假设你有edmx或代码第一个模型:

ctx.Project.Include(p=>p.ProjectComponents).Where(p => p.ProjectComponents.Any(pc=>pc.ComponentID == id)).ToList();

或SQL

SELCT p.* FROM Project as p
INNER JOIN ProjectComponent as pc ON pc.ProjectID = p.ID
WHERE pc.ProjectID = @ID