【发布时间】: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 here。 I 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;
}
期待在这里得到一点帮助。谢谢。
【问题讨论】: