0

我曾经问过一个关于生成树状结构的问题,该结构最终采用了这种方法:

    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
4

1 回答 1

4

编辑:好的,所以你试图返回某种层次结构而不仅仅是一个 flat List<DefaultEventType>,所以你需要改变你的返回类型。

看起来您的“Parent”是Mission,“Child”是Activity,而“ChildChild”是DefaultEventType

基本上,您需要两种自定义类型:

// Holds an Activity, and all the DefaultEventTypes corresponding to it.
class ActivityWithEvents {
    public Activity Activity { get; set; }
    public IEnumerable<DefaultEventType> Events { get; set; }
}

// Holds a Mission, and all the Activities corresponding to it.
class MissionWithActivities {
    public Mission Mission { get; set; }
    public IEnumerable<ActivityWithEvents> Activities { get; set; }
}

或类似的东西。您需要将方法的签名更改为

public IEnumerable<MissionWithActivities> GetTierTree()

和你的回归

var q2 = q.GroupBy(
    // First group the events by Mision
    e => e.Activity.Mission.Id,
    // For each mission found, select a MissionWithActivities
    (mid, events) => new MissionWithActivities {
        Mission = events.First().Activity.Mission,
        Activities = events.GroupBy(
            // Within the Mission, group the events by Activity
            e => e.Activity.Id,
            // For each Activity found, select an ActivityWithEvents
            (aid, events2) => new ActivityWithEvents {
                Activity = events2.First().Activity,
                Tasks = events2
            })
        });
return q2.ToList();

现在你可以遍历每个MissionWithActivities,显示它;并且对于每一个,遍历它的ActivityWithEvents,显示它们;对于每一个,遍历它的DefaultEventTypes,显示它们:

foreach (var mwa in GetTierTree())
{
    Console.WriteLine(mwa.Mission.Name);
    foreach (var awe in mwa.Activities)
    {
        Console.WriteLine("    " + awe.Activity.Name);
        foreach(var e in awe.Events)
            Console.WriteLine("        " + e.Name);
    }
}
于 2012-12-04T16:08:10.277 回答