System.InvalidCastException:无法将类型为“System.Int64”的对象强制转换为“System.String”。 [英] System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.String'.

查看:137
本文介绍了System.InvalidCastException:无法将类型为“System.Int64”的对象强制转换为“System.String”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 var result =(来自dc.PROC_getAppointmentRules()中的x选择x).ToList <   PROC_getAppointmentRulesResult  > (); 
返回结果;





虽然DB列的类型为Varchar

执行循环时我是getin the exception System.InvalidCastException:无法将'System.Int64'类型的对象强制转换为'System.String'。

解决方案

< blockquote>你需要看两件事:

1)什么 dc.PROC_getAppointmentRules 实际返回。



2)将 PROC_getAppointmentRulesResult 与您声明方法所具有的返回类型进行比较。



但你为什么这样做呢?

 来自 x  in  dc.PROC_getAppointmentRules() select  x 

与调用dc.PROC_getAppointmentRules并将结果转换为一个IEnumerable。它必须已经将它用作Linq源...所以这段代码有什么用?


System.InvalidCastException:无法转换类型为'System的对象。 Int64'键入'System.String'。

错误主要是在你完成类型转换错误时给出的





例如:



string a = Convert.tostring(12);





检查你的返回类型,因为我觉得它返回int64数据类型并且你试图将它保存在String数据类型中


var result = (from x in dc.PROC_getAppointmentRules() select x).ToList<PROC_getAppointmentRulesResult>();
                return result;



although DB Column is of type Varchar
While executing loop i am getin the exception "System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.String'."

解决方案

You need to look at two things:
1) What dc.PROC_getAppointmentRules actually returns.
And
2) What PROC_getAppointmentRulesResult is compared with the return type you declared your method as having.

But why are you doing that at all?

from x in dc.PROC_getAppointmentRules() select x

is the same as calling dc.PROC_getAppointmentRules and casting the result as an IEnumerable. Which it has to be already in order to use it as a Linq source...so what use is this code?


"System.InvalidCastException: Unable to cast object of type 'System.Int64' to type 'System.String'."
error is mostly given when you have done a type cast error


for example:

string a=Convert.tostring(12);


check your return type because i feel it is returnin an int64 data type and you are trying to save it in a String datatype


这篇关于System.InvalidCastException:无法将类型为“System.Int64”的对象强制转换为“System.String”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆