开发者

Linq2Sql -How to write group by query

开发者 https://www.devze.com 2022-12-21 21:18 出处:网络
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

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    Ann开发者_如何学Cual    10  5,000.00

2010    Personal  3    1,500.00

2009    Annual    15   10,000.00

2009 etc


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消