SelectMany inside SelectMany

本文关键字:SelectMany inside | 更新日期: 2023-09-27 18:20:40

我尝试了这两种方法,但都不起作用?怎么做?

.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");

SelectMany inside SelectMany

我认为您想要的是通过联接表从Events中选择所有Section。

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