Say I have classes like:
public class ServiceCall
{
public int ServiceCallID {get; set;}
public DateTime ReportedTimestamp {get; set;}
public bool IsPaid {get; set;}
public decimal LabourNet { get; set;}
public decimal LabourVat {get; set;}
}
public class UsedPart
{
public int UsedPartID { get; set; }
public decimal NetPrice { get; set; }
public decimal VatAmount {get; set;}
}
I want to return data in for format:
Month #CallsReceived AmountInvoiced MoneyReceived
Jan 2009 202 €32050.20 €29200.00
Feb 2009 213 €35050.20 €34200.00
Amount invoiced is a total of all the (NetPrices + VatAmounts) + (LabourNet + LabourVat) net a for a month and money received is a total of all where IsPaid = true. I'm having trouble getting the linq query correct for this report. I'm not sure where to have my grouping and when to perform the Sum.
The linq query that I have at the moment is as follows, but I'm not getting anything near what I want at the moment. Anyone with some linqy goodness?
var q = from s in ServiceCalls
join up in UsedParts on s.ServiceCallID equals up.ServiceCallID into tmp
from nullablePart in tmp.DefaultIfEmpty()
group s by s.ReportedTimestamp.Month
into grp
select new
{
Month = grp.Key,
CallsReceived = grp.Count(),
AmountInvoiced = grp.Sum(s => s.UsedParts.Sum(p => p.NetPrice)),
};