c# - C#中var的初始化

标签 c# winforms linq var

考虑下面的代码:

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/

相关文章:

c# - 如何取消选中 WPF (MVVM) 中的单选按钮

c# - 为什么 Select 返回一个 bool 值?

c# - 如何将键与值连接起来?

winforms - Windows 7 下列表框控件的渲染问题

c# - DataGridView SelectLine 颜色为无?

c# - 在 X 时间内在 C# 中加载动画 GIF 图片

C# linq 查询聚合可为空 boolean 值

c# - 在 MonoTouch 中将 PerformSelector 与 @selector 结合使用

c# - DateTime.ToString 删除斜线

javascript - 如何将 .NET URL 正则表达式转换为 Javascript URL 正则表达式?