有人可以帮我编写一个LINQ查询来获取已加入的记录吗?我下面有两个型号。我希望得到属于给定项目的需求计数,并且有一个或多个ProjectTest加入它。
public class ProjectTest
{
public int ProjectTestID { get; set; }
public int ProjectID { get; set; }
public String Objective { get; set; }
public String Category { get; set; }
public String SubCategory { get; set; }
public String Tags { get; set; }
public virtual ICollection<ProjectRequirement> ProjectRequirements { get; set; }
public virtual ICollection<ProjectTestStep> ProjectTestSteps { get; set; }
}
public class ProjectRequirement
{
public int ProjectRequirementID { get; set; }
public int ProjectID { get; set; }
[Display(Name = "Req No.")]
public String ProjectRequirementIDStr { get; set; }
[Display(Name = "Module")]
public String ModuleName { get; set; }
[Display(Name = "Description")]
public String Description { get; set; }
public virtual ICollection<ProjectTest> ProjectTests { get; set; }
}
我只是尝试按如下方式编写,但似乎没有用。
db.ProjectRequirements
.Where(e => e.ProjectID == activeProjectID &&
e.ProjectTests
.Select(ept => ept.ProjectTestID)
.Count() > 0)
.Select(e => e.ProjectRequirementID)
.Count();
答案 0 :(得分:1)
目前,您正在计算具有给定ID并且至少有ProjectRequirement
的{{1}}个对象的数量。
如果您想计算给定ProjectTest
的{{1}} s的数量:
ProjectTest