【问题标题】:SelectMany inside SelectManySelectMany 中的 SelectMany
【发布时间】:2014-09-05 06:30:10
【问题描述】:

这两种方法我都试过了,但没有用?该怎么做?

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

【问题讨论】:

  • 编译器告诉你该做什么。
  • 请举一个简短但完整的例子。我不希望您必须指定类型参数。
  • 这个例子适合你吗,@Jon Skeet?
  • 我认为我们需要查看数据类型 EEvent。即了解 x 在您的初始帖子中的结构以及 x.SectionEvents 是什么
  • 并非如此 - 这远不是一个简短但完整的示例来说明问题,不是吗?特别是我无法复制、粘贴、编译和查看问题。如果你提供了一个很好的例子,那么没有人需要猜测......

标签: c# linq


【解决方案1】:

我想你想要的是通过一个连接表从事件中选择所有部分。

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

【讨论】:

  • 是的,你是对的!第二个 SelectMany 是错误的。谢谢你的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-13
  • 1970-01-01
相关资源
最近更新 更多