Omni
Omni

Reputation: 337

'Could not convert from type' Error

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

Upvotes: 0

Views: 157

Answers (1)

Rawling
Rawling

Reputation: 50104

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 the Activity, and "ChildChild" is the DefaultEventType.

Basically, you need two custom types:

// 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; }
}

or something similar. You will need to change the signature of your method to

public IEnumerable<MissionWithActivities> GetTierTree()

and your return to

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();

Now you can iterate through each MissionWithActivities, displaying it; and for each one, iterate through its ActivityWithEvents, displaying them; and for each one, iterate through its DefaultEventTypes, displaying them:

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);
    }
}

Upvotes: 4

Related Questions