【问题标题】:LINQ - Having trouble getting List of IDs from a List of ObjectModelLINQ - 从 ObjectModel 列表中获取 ID 列表时遇到问题
【发布时间】:2016-12-07 00:08:29
【问题描述】:

我是 LINQ 的新手,我整天都在浏览 Internet 并浏览教程。我有一些我认为可以工作的代码,但它抱怨返回类型。这是我的代码:

// The orgs parameter is a list of all buildings in the entire organization
// I want to retrieve only the IDs of all buildings in a particular region
private IList<int> GetRegionBuildingIDs(int regionId, List<OrgModel> orgs)
{
    var ids = from org in orgs
              where org.regionId == regionId
              select new { id = org.buildingId };
    return (IList<int>)ids;
}

这会返回一个 id 列表,但它们属于 匿名类型,并且强制转换不起作用。我得到一个 System.InvalidCastException。

我发现最接近我的问题的答案仍然让我感到困惑。 It's hereI tried to follow the answer but my select(org.buildingId) only offers .ToString

所以这是我最近的尝试,但它是错误的:

private IList<int> GetRegionBuildingIDs(int regionId, List<OrgModel> orgs)
{
    IEnumerable<int> ids = from org in orgs
              where org.regionId == regionId
              select (org.buildingId)._________;  // This should be .ToList
    return (IList<int>)ids;
}

期待在这里得到一点帮助。谢谢。

【问题讨论】:

    标签: c# linq


    【解决方案1】:

    你不需要select new {},你可以直接select它:

    var ids = from org in orgs
              where org.regionId == regionId
              select org.buildingId;
    
    return ids.ToList();
    

    这是因为 select new {} 使用带有单个 int 成员的匿名类型,而不是直接返回 int 值。

    您也不能将 Linq 查询强制转换为 List&lt;T&gt;,因为它们不是 List&lt;T&gt; 的实例,而是其他东西(围绕 yield return C# 语言特性构建的延迟评估状态机)。

    当然,你的代码可以变得更简单

     private static IEnumerable<Int32> GetRegionBuildingIds(Int32 regionId, IEnumerable<OrgModel> orgs) {
    
        return orgs
            .Where( org => org.regionId == regionId )
            .Select( org => org.buildingId );
    }
    

    如果您愿意,可以制作自己的扩展方法:

    public static IEnumerable<Int32> GetRegionBuildingIds(this IEnumerable<OrgModel> orgs, Int32 forRegionId) {
    
        return orgs
            .Where( org => org.regionId == regionId )
            .Select( org => org.buildingId );
    }
    

    这样使用:

    IEnumerable<Orgs> orgsModel = GetFromDatabaseOrWhateverYourBackingStoreIs();
    return orgsModel.GetRegionBuildingIds( forRegionId: 123 );
    

    【讨论】:

      【解决方案2】:

      就选角而言。试试这个:

      // The orgs parameter is a list of all buildings in the entire organization
      // I want to retrieve only the IDs of all buildings in a particular region
      private IList<int> GetRegionBuildingIDs(int regionId, List<OrgModel> orgs)
      {
          var ids = (from org in orgs
                    where org.regionId == regionId
                    select (int)org.buildingId).ToList();
          return ids;
      }
      

      或者如果 buildingId 可以为空,你可以试试这个

      select (int)(org.buildingId ?? 0)
      

      【讨论】:

      • 这不会编译。带括号的 Linq 语句的类型为 IEnumerable&lt;'a { Int32 id }&gt;,与 IEnumerable&lt;Int32&gt; 不兼容。
      猜你喜欢
      • 2011-07-21
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多