我在一个C#ASP.NET项目。System.InvalidCastException:指定的转换无效。错误
我有一个类型为int
的userid字段的MySQL表。
现在我想要得到使用LINQ的userid值等于某个值的行数。
要做到这一点,我写了下面的方法:
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;
}
但它显示出红色高亮区域count = (from x in mytable.AsEnumerable() where x.Field<Int32>("userid") == usercode select x).Count();
错误System.InvalidCastException: Specified cast is not valid.
。
我不知道我在这里做错了什么。请帮忙。
'mysql> desc vticketuser; + -------------- + ------------------ + ------ + ----- + - -------- + ------- + |字段|类型|空| Key |默认|额外| + -------------- + ------------------ + ------ + ----- + - -------- + ------- + | idticket | int(10)unsigned | NO | | 0 | | |用户ID | int(10)unsigned | NO | | NULL | | ' 这是字段 – user2168042
@ user2168042您将数据库定义与托管代码时的值混合在一起。尝试查看代码中原始的''userid''值,调用'.GetType'并查看它创建的类型为 – JaredPar
我不知道我是否正确地做它..但我试过像这样 ' string ty = t.Rows [0] [“userid”]。GetType()。ToString();' 它说“System.UInt32” – user2168042