2014-09-05 93 views
0

我試過這兩個,但沒有工作?怎麼做?SelectMany裏面SelectMany

.SelectMany(x => x.SectionEvents).SelectMany(t => t.Section) 

.SelectMany(x => x.SectionEvents.SelectMany(t => t.Section)) 

錯誤:

的類型參數方法 'System.Linq.Enumerable.SelectMany<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,System.Collections.Generic.IEnumerable<TResult>>)' 不能從使用中推斷出來。嘗試明確指定類型參數 。

EEvent.List<EEvent>("CalendarPeriodUId", ECalendarPeriod.Current().UId).Value.ToList() 
       .SelectMany(x => x.SectionEvents.SelectMany(t => t.Section)).ToFCollection().Bind(ddlSection, "SectionName"); 
+0

編譯器告訴你該怎麼做。 – 2014-09-05 06:33:30

+0

請給出一個簡短但完整的例子。我不希望你必須指定類型參數。 – 2014-09-05 06:40:49

+0

就是適合你的這個例子@Jon Skeet? – Mert 2014-09-05 06:52:50

回答

2

我想你想通過一個連接表來選擇活動的所有部分。

public class Event 
{ 
    public ICollection<SectionEvent> SectionEvents { get; set; } 
} 
public class SectionEvent 
{ 
    public Event Event { get; set; } 
    public Section Section { get; set; } 
} 
public class Section 
{ 
    public ICollection<SectionEvent> SectionEvents { get; set; } 
} 

如果是這樣,那麼你需要的是SelectManySelect

var q = events.SelectMany(e => e.SectionEvents).Select(se => se.Section); 
+0

不錯的猜測!!!!! – Mick 2014-09-05 07:00:22

+0

是的,你是對的!第二個SelectMany錯了。感謝您的幫助 :) – Mert 2014-09-05 07:00:55