I had a asked a previous question about generating a tree like structure which culminated in this method:
public ICollection<DefaultEventType> GetTierTree()
{
var q = from mission in _context.tMissions
join activity in _context.tActivities on mission.id equals activity.missionId
join project in _context.tDefaultEventTypes on activity.id equals project.activityId
where !project.isRemoved && project.defaultCategoryId == 4
orderby mission.name
select new DefaultEventType(project.tierLevel.TryParseEnum<GanttType>(GanttType.Unknown), DefaultCategoryRepository.CreateFrom(project.tDefaultCategory))
{
AllowNumericSuffix = project.allowNumericSuffix,
AttachMilestoneMoniker = project.attachMilestoneMoniker,
Description = project.description,
Id = project.id,
IsReadOnly = project.isReadOnly,
IsSticky = project.isSticky,
Name = project.name,
Sid = project.sid,
Style = project.style.TryParseEnum<GanttElementStyle>(GanttElementStyle.Unknown),
TimeStamp = project.createdDT,
UpdatedTimeStamp = project.updatedDT,
Activity = new Activity { Id = activity.id, Name = activity.name, Mission = new Mission { Id = mission.id, Name = mission.name } }
};
var q2 = q.GroupBy(row => row.Activity.Mission.Id)
.Select(group => group.GroupBy(row => row.Activity.Id));
return q2.Cast<DefaultEventType>().ToList();
}
It fails on the return. I’ve done a ton of googling and searching the board. Obviously the types aren’t matching up because of something with the GroupBy, but I don’t understand exactly why this doesn’t work.
Can anyone provide insight or how I would go about fixing this?
Edit:
I’m trying to output a tree like structure. Currently it is looking like this:
* Parent1
* Child1
*ChildChild1
* Parent1
* Child1
* ChildChild2
* Parent1
* Child1
* ChildChild3
What I want it to do is group things like so:
* Parent1
* Child1
* ChildChild1
* ChildChild2
* ChildChild3
Edit: OK, so you’re trying to return some kind of hierarchical structure rather than just a flat
List<DefaultEventType>, so you’ll need to change your return type.It looks like your “Parent” is the
Mission, “Child” is theActivity, and “ChildChild” is theDefaultEventType.Basically, you need two custom types:
or something similar. You will need to change the signature of your method to
and your return to
Now you can iterate through each
MissionWithActivities, displaying it; and for each one, iterate through itsActivityWithEvents, displaying them; and for each one, iterate through itsDefaultEventTypes, displaying them: