【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List<System.Collections.Generic.IEnumerable<long>>' to <long>'无法将类型 'System.Collections.Generic.List<System.Collections.Generic.IEnumerable<long>>' 隐式转换为 <long>'
【发布时间】:2015-08-21 14:22:31
【问题描述】:

我在我的 ASP.NET MVC Web 应用程序中编写了以下 LINQ 语句:

List<long> ids = TServers
  .Select(a => a.TVirtualMachines
     .Select(a2 => a2.Technology.ID.Value))
  .ToList();

我想要实现的是执行以下操作:

我们有 Tservers ,它有一个 TVirtualMachines 的集合,每个 TVirtualMachine 都与 on Technology 相关联。我想为 Tserver 下的所有 TVirtualMachine 选择 Technology.ID。但我上面的代码会返回以下错误:

无法将类型“System.Collections.Generic.List>”隐式转换为“System.Collections.Generic.List”

【问题讨论】:

标签: c# asp.net linq entity-framework entity-framework-6


【解决方案1】:

你需要SelectMany:

List<long> ids = 
  TServers.SelectMany(a => 
    a.TVirtualMachines.Select(a2 => a2.Technology.ID.Value)).ToList();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2013-05-14
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多