I have a problem with this query trying to get a sum and a single value from the same child
Class 1
class Project{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<SubProject> SubProjects { get; set; }
}
Class 2
class SubProject{
public int Id { get; set; }
public string Description { get; set; }
public IEnumerable<Week> Weeks { get; set; }
}
Class 3
class Week{
public int Week { get; set; }
public int Hours { get; set; }
}
what i’m trying to do is get all weeks, within a range, for a Project in a new list of classes like this
class ProjectOverview{
public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<ProjectWeekOverview> Weeks { get; set; }
}
class ProjectWeekOverview{
public int Week { get; set; }
public int TotalHours { get; set; }
}
i have tried something like this, but i cant manage to get in the week for it also, if what i done this far is even correct
List<Project> projects = { List.Of.Projects };
List<ProjectOverview> overview = projects
.Select(p => new ProjectOverview
{
Id = p.Id,
Name = p.Name,
Weeks = p
.SubProjects
.Select(sp => new ProjectWeekOverview
{
Week = ????,
TotalHours = sp
.Where(w => w.Week >= 30 && w.Week <= 35)
.Sum(w => w.Hours)
})
})
.ToList();
anyone that can help with this?
Edit: a subproject can contain multiple week items with the same week like, to store hours from different action how long they took
week { week = 10, hours = 3 }
week { week = 10, hours = 4 }
week { week = 11, hours = 3 }
so what i want is to have the week property to a set week, and the totalhours to sum of all hours in that week
You need to GroupBy week, and have Week as the key and Sum as item.
Give this a try. The output is an anonymous type, must have to convert it to whatever type you want as result. Its the grouping that matters.