C#中var的初始化

2 c# linq var winforms

考虑以下代码:

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)
{

}
Run Code Online (Sandbox Code Playgroud)

如何初始化var ids我可以使用它来调用GetPopulation()

Jon*_*eet 5

那么,主要问题与使用"var"无关.你有一个带有在其中声明的变量的foreach循环,然后你试图使用该变量从循环外部返回.您期望价值是什么?

如果您尝试选择所有国家/地区,为什么不这样做:

public IEnumerable <Country> ListPopulation()
{
    return GetPopulation(context.continentTable.Select(x => x.countryId));
}
Run Code Online (Sandbox Code Playgroud)

迭代每个大陆的重点是什么?或者大陆表中有哪些国家未被大陆财产引用而您未展示?