使用LINQ求和的属性 [英] Sum properties using LINQ
本文介绍了使用LINQ求和的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
DealsThisMonthOpen = deals.Where(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Open").Count(),
DealsThisMonthLost = deals.Where(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Lost").Count(),
DealsThisMonthWon = deals.Where(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Won").Count(),
DealsThisMonth = DealsThisMonthOpen + DealsThisMonthLost + DealsThisMonthWon;
最后一行语法不正确.是否可以这样做,否则我将为此属性编写查询以计算总和?
The last line is not syntax correct. Is it possible to do it like this or I will have to write query for this Property to calculate sum?
谢谢
推荐答案
btw,Count()
也支持predicate
参数:
http://msdn.microsoft.com/en-us/library/bb535181. aspx
btw, Count()
also supports predicate
argument:
http://msdn.microsoft.com/en-us/library/bb535181.aspx
因此您可以:
var a = deals.Count(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Open"),
var b = deals.Count(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Lost"),
var c = deals.Count(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Won"),
new foobar
{
DealsThisMonthOpen = a,
DealsThisMonthLost = b,
DealsThisMonthWon = c,
DealsThisMonth = a + b + c
};
这篇关于使用LINQ求和的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文