Malcolm
Malcolm

Reputation: 12864

Linq2Sql -How to write group by query

If I have the following fields in a table called DailyLeaveLedger

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

Upvotes: 1

Views: 314

Answers (2)

AxelEckenberger
AxelEckenberger

Reputation: 16936

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.

Upvotes: 2

Axarydax
Axarydax

Reputation: 16613

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

Upvotes: 2

Related Questions