考虑下面的代码:
public IEnumerable <Country> ListPopulation()
{
foreach(var continent in Continents)
{
var ids = context.continentTable
.where(y=>y.Name == continent.name)
.select(x=>x.countryId);
}
return GetPopulation(ids);// ids is not available here
}
Public IEnumerable<Country>GetPopulation(IQueryable<int> idnumbers)
{
}
如何初始化 var ids
以便我可以使用它来调用 GetPopulation()
?
最佳答案
好吧,主要问题与使用“var”无关。您有一个 foreach 循环,其中声明了变量,然后您尝试使用该变量从循环的外部 返回。您期望值是多少?
如果您要选择所有国家,为什么不这样做:
public IEnumerable <Country> ListPopulation()
{
return GetPopulation(context.continentTable.Select(x => x.countryId));
}
遍历每个大陆有什么意义?或者 continentTable 中是否存在未被您未显示的 Continents 属性引用的国家?
关于c# - C#中var的初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/484562/