我想显示这样的树结构:
使命1
- 活动1
- 项目1
- 活动2
- 活动1
任务2
- 活动3
- 项目2
- 活动3
但相反,我得到一个列表,该列表重复每个活动或项目的父元素,如下所示:
使命1
- 活动1
- 项目1
- 活动1
使命1
- 活动2
我想将活动和项目分组在一个父(“任务”)元素下。
这是我的 LINQ 查询:
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 CreateFrom(project);
return q.ToList();
然后我将它绑定到一个列表并显示在页面上。这是我的aspx:
<asp:DataList ID="dlTierTypes" runat="server">
<ItemTemplate>
<li><%# Eval("Activity.Mission.Name") %></li>
<ul>
<li><%# Eval("Activity.Name") %></li>
<ul>
<li><%# Eval("Name") %></li>
</ul>
</ItemTemplate>
</asp:DataList>
后面的代码只是绑定它:
public void SetTierTree(IEnumerable<DefaultEventType> tierList)
{
dlTierTypes.DataSource = tierList;
dlTierTypes.DataBind();
}
不知道如何准确解决这个问题。
编辑 - 这是我在存储库中的新功能。
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))
{
//t = 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();
}
但我在显示页面时收到此错误:“无法从类型 'System.Collections.Generic.List 1[System.Linq.IGrouping
2[System.Int32,DefaultEventType]]' 转换为类型 'DefaultEventType'。”