C# System.InvalidCastException: 指定的强制转换无效。错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18421733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.InvalidCastException: Specified cast is not valid. Error
提问by user2168042
I am on a C# ASP.NET project.
我在一个 C# ASP.NET 项目上。
I have a MySQL table with a userid field of type int
.
我有一个带有 userid 字段类型的 MySQL 表int
。
Now I want to get the number of rows where value of userid equals certain value using LINQ.
现在我想使用 LINQ 获取 userid 值等于特定值的行数。
To achieve this, I wrote the following method:
为此,我编写了以下方法:
public int getCount(int usercode) {
int count = 0;
DataTable mytable = getAllRowsAndReturnAsDataTable(); // assigning a DataTable value to mytable.
if (mytable.Rows.Count > 0) {
count = (from x in mytable.AsEnumerable() where x.Field<Int32>("userid") == usercode select x).Count();
}
return count;
}
but it is showing error System.InvalidCastException: Specified cast is not valid.
showing count = (from x in mytable.AsEnumerable() where x.Field<Int32>("userid") == usercode select x).Count();
in red highlight area.
但它显示错误System.InvalidCastException: Specified cast is not valid.
显示count = (from x in mytable.AsEnumerable() where x.Field<Int32>("userid") == usercode select x).Count();
在红色突出显示区域。
I don't know what I did wrong here. Please help.
我不知道我在这里做错了什么。请帮忙。
采纳答案by JaredPar
The most likely cause of the InvalidCastException
is the x.Field<Int32>("userid")
line. The Field<T>
extension method will throw an InvalidCastException
in the case where the actual type of the data doesn't match the type which was passed to Field<T>
. Hence if userid
wasn't an Int32
this would throw.
最可能的原因InvalidCastException
是x.Field<Int32>("userid")
线路。该Field<T>
扩展方法将抛出InvalidCastException
的情况下的数据的实际类型不匹配将其传递给类型Field<T>
。因此,如果userid
不是Int32
this 会抛出。
EDIT
编辑
Based on your comments the type of userid
is actually UInt32
and not Int32
. This is what is causing the problem. Try using the following and it should work
根据您的评论,类型userid
实际上是UInt32
而不是Int32
。这就是导致问题的原因。尝试使用以下方法,它应该可以工作
x.Field<UInt32>("userid")
回答by BrutalDev
Without looking at the data coming back from your database I can only guess that the following part of your LINQ is at fault:
如果不查看从数据库返回的数据,我只能猜测 LINQ 的以下部分有问题:
x.Field<Int32>("userid")
Your userid column value probably isn't an int, I would put my money on it being NULL?
您的 userid 列值可能不是 int,我会把钱花在它是 NULL 上吗?
UPDATE: Can you confirm it's not the Field call that is breaking? Simply change your code to something like this without the Field call:
更新:您能确认不是 Field 呼叫中断吗?只需将您的代码更改为类似这样的内容,无需 Field 调用:
public int getCount(int usercode){
int count = 0;
DataTable mytable = getAllRowsAndReturnAsDataTable(); // assigning a DataTable value to mytable.
if (mytable.Rows.Count > 0) {
count = mytable.AsEnumerable().Count(); // No WHERE function call so no casting.
}
return count;
}
You could also inspect what the values are that are returned by mytable.AsEnumerable() in a watch window for example to ensure that everything looks correct. If the code above works then it's the Field call blowing up. Find out which row cannot be cast to an Int32 and go from there.
例如,您还可以在监视窗口中检查 mytable.AsEnumerable() 返回的值是什么,以确保一切看起来都是正确的。如果上面的代码有效,那就是 Field 调用爆炸了。找出哪一行不能转换为 Int32 并从那里开始。
If it is in fact NULL, there are a number of ways you can solve this.
如果它实际上是 NULL,则有多种方法可以解决此问题。
- Make sure you don't return NULL from your database query, in MySQL you can use IFNULL.
Use a nullable type for the generic being passed into Field:
where x.Field("userid") == (Int32?)usercode
- 确保您没有从数据库查询中返回 NULL ,在 MySQL 中您可以使用IFNULL。
为传递到 Field 的泛型使用可空类型:
其中 x.Field("userid") == (Int32?)usercode