linq查询返回唯一项的嵌套列表

时间:2015-04-17 08:09:45

标签: c# linq linq-to-sql

我有一个linq查询:

            var leaveDetails = (from l in leaveList
            group l by new {l.LeaveDateStart.Year, l.LeaveType}
            into yearGroup
            select new 
            {
                yearGroup.Key.Year,
                yearGroup.Key.LeaveType,
                LeaveTaken = yearGroup.Sum(l => EntityFunctions.DiffDays(l.LeaveDateStart, l.LeaveDateEnd))

            }).ToList();

结果如下所示:

Image showing query results 但是我想分开一年。因此,对于每个独特的年份,我都会​​列出休假类型和休假日期。

我试过在javascript的前端做这件事,但看起来很乱。有没有办法可以在C#或linq查询中执行此操作。感谢

1 个答案:

答案 0 :(得分:1)

您有一个按年份和假类型分组的当前结果。您可以简单地按年度对当前结果进行分组,并选择保留类型和保留为每组值。

像这样(注意:尚未测试过):

var leaveDetailsPerYear = (from l in leaveDetails
                           group l by l.Year
                           into yearGroup
                           select new {
                                Year = yearGroup.Key,
                                Data = yearGroup.ToList()
                           }
                          )
                          .ToList();