我向PassAllocations类添加了以下属性:
[NotMapped]
public int PendingForApprovalCount { get; set; }
var passAllocations = db.PassAllocations.Include(p => p.PassRule)
.Where(p => p.CompanyID == company.CompanyID).ToList();
我试图用PendingForApprovalCount
的结果填充passAllocations列表中的每个db.PassLinkings.Where(p => p.PassAllocationID == parentid and p.status="Pending").ToList()
属性
在每个PassAllocationID
列表中,{parent}是passAllocations
答案 0 :(得分:0)
passAllocations
.ForEach(x=>x.PendingForApprovalCount=db.PassLinkings
.Where(p => p.PassAllocationID == x.id &&
p.status="Pending")
.Count())
这可能会为您提供正确的结果,但这不是一个明智的解决方案,因为它会导致对db的多次点击,最终会影响性能。
var desiredResult=(from a in db.PassAllocations
.Include(pa => pa.PassRule)
.Where(pa => pa.CompanyID == company.CompanyID)
join b in db.PassLinkings
.Where(pl => pl.status="Pending")
.GroupBy(pl=>pl.PassAllocationID)
on a.PassAllocationID equals b.Key
select new {a,b})
.Select(x=>new PassAllocation
{
PassAllocationID=x.a.PassAllocationID,
.
.
. <------------Other Properties
.
.
PendingForApprovalCount=x.b.Count()
}).ToList();
希望有帮助。