'无法转换类型'错误

时间:2012-12-04 16:01:54

标签: c# linq type-conversion

我曾经问过一个关于生成树状结构的问题,这个问题在这个方法中达到了顶峰:

    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

1 个答案:

答案 0 :(得分:4)

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

看起来您的“父母”是Mission,“儿童”是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);
    }
}