Linq query to return nested list for unique items

I have a linq query like:

            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();

      

With the results shown below:

Image showing query results However, I want to group the fiscal year. So for each unique year, I have a list of vacation types and vacation days.

I've tried doing this in the frontend in javascript, but it looks messy. Is there a way to do this in C # or in a linq query. Thanks to

+3


source to share


1 answer


You have a current result that is grouped by year and type of vacation. You can simply group your current result by year and select the type of vacation and leave it as a value for each group.

Like this (note: didn't test it):



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

      

+1


source







All Articles