tags:

views:

41

answers:

2

Hi,

If I have the following fields in a table called DailyLeaveLedger

  • LeaveDate
  • LeaveType
  • AmountPaid

How do I write a linq2sql group by query to get this result where it groups by year and Leavetype and gives a column of the count of leave days and the sum of amount paid?? Sorted in descending order of year.

2010    Annual    10  5,000.00

2010    Personal  3    1,500.00

2009    Annual    15   10,000.00

2009 etc
+2  A: 

DailyLeaveLedgers.OrderBy(p=>p.LeaveDate.Year).GroupBy(p => new { p.LeaveDate.Year, p.LeaveType });

Axarydax
Thanks how do I get the count and sum amount paid?
Malcolm
+1 for the combined group key.
Obalix
+2  A: 

Building on Axarydax' result:

var result = DailyLeaveLedgers.OrderBy(p=>p.LeaveDate.Year)
                              .GroupBy(p => new { 
                                  Year = p.LeaveDate.Year, 
                                  Type = p.LeaveType 
                              })
                              .Select(g => new { 
                                  Year = g.Key.Year, 
                                  Type = g.Kye.Leave, 
                                  Count = g.Count(),
                                  Sum = g.Sum(x => x.AmmountPaid)
                              });

result now holds a enumeration of objects from with the requested data.

Obalix
beat me to it ;) so I won't update my post
Axarydax