This question already has an answer here:
- Pivot an observable collection [duplicate] 3 answers
- Pivot data using LINQ 5 answers
I have the following Dynamic List
Crew NameSurname Period Result
ABC John DOE Q1 54,09
ABC John DOE Q2 59,57
ABC John DOE Q3 62,11
How can I get this result with in linq.
Crew NameSurname Q1 Q2 Q3
ABC John DOE 47,51 47,51 51,46
I've tried this way but I couldn't get the result
List.GroupBy(c => c.PersonnelID)
.Select(g => new
{
PersonnelID = g.Key,
Period1 = g.Where(c => c.Period == 1).Sum(c => c.Result),
Period2 = g.Where(c => c.Period == 2).Sum(c => c.Result),
Period3 = g.Where(c => c.Period == 3).Sum(c => c.Result)
});
You can do this:
But you will need to define a new class. Something like: