Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用Linq中的计数填充NotMapped属性?_C#_Linq - Fatal编程技术网

C# 如何用Linq中的计数填充NotMapped属性?

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

我已将以下属性添加到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.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参考资料或使用第一批代码,这是不可取的