tags:

views:

55

answers:

1

I have a collection of order objects (properties - date, amount and vendor). I need to prepare a report showing spend by vendor by month since 01/01/2009. How can i get the results using LINQ?

+1  A: 

Something like:

var minDate = new DateTime(2009, 1, 1);

var query = from order in db.Orders
            where order.Date >= minDate
            group order by new { order.Vendor, 
                                 order.Date.Month, order.Date.Year } into g
            select new { g.Key.Vendor, g.Key.Month, g.Key.Year,
                         g.Sum(x => x.Amount) };

That will group by vendor, month and year.

Jon Skeet
i need it from 1/1/2009 to current date(date whenever report is run). Is there a better way of doing this? Thanks, ken
Ken
@user441746: Okay, I've edited it... is that what you're after?
Jon Skeet
I need a result set with 3 columns "Vendor, Month, Amount(sum) spent for that month for that vendor" . Is there a way that i can get all the results in one query run? I mean i do not want to run it for every month separately using a loop or similar. I hope I have explained clearly what I am trying to achieve.
Ken
btw, jon, your "C# in depth" book is one of my best reads. great work. I am so thrilled to receive a response from you. Thanks a bunch,
Ken
@user441746: Edited to give you the sum of the amount... hope this helps... and I'm glad you've enjoyed the book :)
Jon Skeet