If I understand the question, you want a mapping of Task to State for each Robot. You could group by Robot and select a dictionary for each group:
Logs.GroupBy(t => t.Robot).Select(g => new {
Robot = g.Key,
TaskStates = g.ToDictionary(t => t.Task, t => t.State)
})
This assumes that task names are unique for each robot (ToDictionary
would throw an exception otherwise).
You could also add another level of grouping for dates:
Logs.GroupBy(t => t.LogDate).Select(g => new {
Date = g.Key,
Details = g.GroupBy(t => t.Robot).Select(g => new {
Robot = g.Key,
TaskStates = g.ToDictionary(t => t.Task, t => t.State)
}).ToList()
})
Note that the Details
property is essentially equivalent to my first example, the only difference being that it queries the outer grouping instead of the whole sequence. The result is a sequence of {Date, Details} where each "detail" is a list of {Robot, TaskStates}.
I haven't tested this, so let me know if there are any bugs I missed.