我在一个C#ASP.NET项目上.
我有一个带有userid字段类型的MySQL表int.
现在我想使用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;
}
Run Code Online (Sandbox Code Playgroud)
但它System.InvalidCastException: Specified cast is not valid.显示count = (from x in mytable.AsEnumerable() where x.Field<Int32>("userid") == usercode select x).Count();在红色高亮区域显示错误.
我不知道我在这里做错了什么.请帮忙.