尝试将db查询转换为字符串时出错

时间:2012-08-15 13:36:30

标签: vb.net asp.net-mvc-3

我试图从数据库中提取一个整数,但是出现错误

我在控制器中的代码是:

        Dim SeqNumQuery = From a In db.RequestedServices
                          Where a.RequestedServiceId = id
                          Select a.StationId

        Dim StationId As Integer = SeqNumQuery.ToString

错误是:

范围变量'StationId'隐藏先前在查询表达式中定义的封闭块或范围变量中的变量。

1 个答案:

答案 0 :(得分:3)

我认为问题在于您的变量StationId正在某个范围内声明。尝试更改为:

Dim SeqNumQuery = From a In db.RequestedServices
                          Where a.RequestedServiceId = id
                          Select a.StationId

StationId = SeqNumQuery.ToString

但是看看正在使用的其他变量是什么。如果您需要,请使用StationId以外的其他内容作为变量名称,例如RequestedServicesStationId

有关此错误的更多信息:

Range variable hides a variable in an enclosing block or a range variable previously defined in the query expression.


另外,我不明白为什么使用.ToString扩展方法。这不会导致您的脚本抛出异常,但我建议将其更改为CInt()

StationId = CInt(SeqNumQuery)