I have the following class structure:
class Employee()
{
public String Name { get; set; }
public List<WorkDay> WorkDays { get; set; }
}
class WorkDay()
{
public DateTime Date { get; set; }
public Int Hours { get; set; }
}
Is it possible to pivot the List<Employee>
with Linq so i have result like this in my DataGridView:
| Name | Name |...| Name |
Date | Hours | Hours | | Hours |
Date | Hours | Hours | | Hours |
Date | Hours | Hours | | Hours |
Date | Hours | Hours | | Hours |
... | Hours | Hours | | Hours |
It's tricky because it's two nested lists and i only found examples with single list which are pretty straightforward. Is it possible to Pivot data using LINQ?
I got to this point but it's not quite there yet:
var _result = Employees.SelectMany(x => x.WorkDays)
.GroupBy(x => x.Date)
.Select(y => new
{
DATE = y.Key,
NAME = y.Select(z => z.Employee.Name).ToArray()
})
.ToList();
I would appreciate any suggestions.