我曾经问过一个关于生成树状结构的问题,该结构最终采用了这种方法:
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();
}
返回时失败。我已经做了很多谷歌搜索和搜索板。显然,由于 GroupBy 的某些原因,类型不匹配,但我不明白为什么这不起作用。
谁能提供见解或我将如何解决这个问题?
编辑:我正在尝试输出树状结构。目前它看起来像这样:
* Parent1
* Child1
*ChildChild1
* Parent1
* Child1
* ChildChild2
* Parent1
* Child1
* ChildChild3
我想要它做的是这样分组:
* Parent1
* Child1
* ChildChild1
* ChildChild2
* ChildChild3