2012-12-04 84 views
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

編輯:好了,你試圖返回某種層次結構,而不僅僅是一個公寓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); 
    } 
} 
+0

同意,看起來像排序是什麼OP需要 –

+0

排序。我更新了原件以顯示我想要做的事情。 – Omni

+0

@ negzero7看起來我錯了;我已經更新了這個,讓你得到一個層次結構,而不是一個扁平的結構。 – Rawling