Search code examples
linqsumlinq-group

Query to sum on grouped field


I've a list of tasks. Ie I've struct like

struct Task
{
    public DateTime Completed { get; set; }
    public string TaskKind { get; set; }
    public float Effort { get; set; }
}

And I've a IList<Task>.

Now I want to group the tasks based on the date and group by TaskKind on each day with the summed effort. For eg for I want to print like the below:

01-09-09     Design:10     Coding:5    Coordination:15
02-09-09     Coding:5       Meeting:2

Can anyone help me in constructing a LINQ query to achieve this?


Solution

  • This is a combination of a simple aggregate (COUNT by TaskKind) and a PIVOT. Take a look at this question: Is it possible to Pivot data using LINQ?