【问题标题】:Cannot convert IQueryable<IEnumerable<string>> to return type IEnumerable<string>无法将 IQueryable<IEnumerable<string>> 转换为返回类型 IEnumerable<string>
【发布时间】:2013-07-11 08:15:13
【问题描述】:

在以下函数中,当我尝试返回一个值时出现错误:

无法转换 System.Linq.IQueryable&lt;System.Collections.Generic.IEnumerable&lt;string&gt;&gt; 返回类型System.Collections.Generic.IEnumerable&lt;string&gt;

public IEnumerable<string> GetModuleKindPropertyNames(long moduleKindId)
{
    var configurationId = GetModuleKindPertypeConfigurationId(moduleKindId);
    var propertyNames = _dbSis.ModuleKinds
                              .Where(t => t.PerTypeConfigurationId == configurationId)
                              .Select(x => x.PerTypeConfiguration.Properties
                              .Select(z => z.Name));

    return propertyNames; //red line below property names
}

我该如何解决这个问题?

【问题讨论】:

    标签: c# .net linq ienumerable iqueryable


    【解决方案1】:

    您似乎想从集合中的集合中选择项目,并将结果展平到单个序列中。

    从概念上讲,类似于:

    如果是这种情况,您正在寻找SelectMany 方法:

    var propertyNames = _dbSis.ModuleKinds
                              .Where(t => t.PerTypeConfigurationId == configurationId)
                              .SelectMany(x => x.PerTypeConfiguration.Properties)
                              .Select(z => z.Name);
    

    【讨论】:

    • 基本上 ModuleKind 有 long? PertypeConfigurationId 和 public PropertyConfigurationDefinition PerTypeConfiguration{ get; set; },PropertyConfigurationDefinition 有 public ICollection&lt;Property&gt; Properties { get { return _properties; } set { _properties = value; } },最后 Property 有 public string Name { get; set; }
    • 会尝试你所说的并会得到回报
    猜你喜欢
    • 2011-06-11
    • 1970-01-01
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-04
    • 1970-01-01
    相关资源
    最近更新 更多