C# 如何用Linq中的计数填充NotMapped属性?
我已将以下属性添加到PassAllocations类:C# 如何用Linq中的计数填充NotMapped属性?,c#,linq,C#,Linq,我已将以下属性添加到PassAllocations类: [NotMapped] public int PendingForApprovalCount { get; set; } var passAllocations = db.PassAllocations.Include(p => p.PassRule) .Where(p => p.CompanyID == company.CompanyID).ToList(); 我试图用db.PassLinkings.Where(p=>p
[NotMapped]
public int PendingForApprovalCount { get; set; }
var passAllocations = db.PassAllocations.Include(p => p.PassRule)
.Where(p => p.CompanyID == company.CompanyID).ToList();
我试图用db.PassLinkings.Where(p=>p.PassAllocationID==parentid和p.status=“Pending”).ToList()的结果填充列表passAllocations中的每个PendingForApprovalCount
属性
其中,在每个passAllocations
列表中,“parentid”是PassAllocationID
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();
var desiredResult=(来自db.PassAllocations中的a)
.Include(pa=>pa.PassRule)
.Where(pa=>pa.CompanyID==company.CompanyID)
在db.PassLinkings中加入b
.Where(pl=>pl.status=“待定”)
.GroupBy(pl=>pl.PassAllocationID)
在a.PassAllocationID等于b.Key上
选择新的{a,b})
.选择(x=>新通道分配
{
PassAllocationID=x.a.PassAllocationID,
.
.
。请发布您的PassLocation
类和您想从中提取PendingForApprovalCount
的类。实际上,我正在寻找一种方法,以避免显式绑定其他属性
@Arbaaz。那么,我的朋友,您应该拥有foreignKey参考资料或使用第一批代码,这是不可取的