tags:

views:

61

answers:

3
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?

Thanks

A: 

Have you considered an extension method? Where you provide the sum feature.

Lucas B
+2  A: 

btw, Count() also supports predicate argument: http://msdn.microsoft.com/en-us/library/bb535181.aspx

So you can:

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
};
abatishchev
+2  A: 

Maybe I'm missing something, but that should definitely work.

The Count() method returns an int so DealsThisMonthOpen, DealsThisMonthLost, and DealsThisMonthWon are all integer values. DealsThisMonth is just the sum of those three integer values.

You could also make it a little cleaner (unless you need the three distinct values for something else later:

var dealsThisMonth = deals
    .Count(d => d.DateCreated.Month == date.Month
        && (new string[] { "Open", "Lost", "Won" }).Contains(d.DealStatus));
Justin Niessner