Bad*_*Dub 1 c# linq dictionary model sum
因此,在使用linq时,我总是遇到过这种情况.说我有如下代码:
var results = _context.Products.Select(d =>
new Models.ProductData()
{
Id = d.ProductId,
Name = d.Product.Name,
Qty = d.Number.Where(y => y.Active && y.CodeId == 5 && y.Status == "NEW").Sum(y => y.FeeAmount),
Qty1= d.Number.Where(y => y.Active && y.CodeId == 7 && y.Status == "NEW").Sum(y => y.Fee),
Total = d.Number.Where(y => y.Active && y.CodeId == 5 && y.Status == "NEW").Sum(y => y.FeeAmount) + d.Number.Where(y => y.Active && y.CodeId == 7 && y.Status == "NEW").Sum(y => y.Fee)
}
);
Run Code Online (Sandbox Code Playgroud)
对于总值,有没有更好的方法来计算总数而不必写出所有带有+符号的linq语句?能够写出像这样的东西会好得多
Total = Qty + Qty1
Run Code Online (Sandbox Code Playgroud)
我可以省略初始映射中的总数和重新映射它按Id分组并通过引用Total = Qty + Qty1来分配总数,但这似乎只是四舍五入.
任何建议表示赞赏.
在您的Models.ProductData类中,Total按如下方式定义属性:
public double Total {
get {
return this.Qty + this.Qty1;
}
}
Run Code Online (Sandbox Code Playgroud)
并避免在Linq查询中设置它.