[英]System.InvalidCastException: Specified cast is not valid (linq query)
Specified cast is not valid error at line: select new File.Models.FileDetail(). 指定的强制转换在第1行无效错误:选择新的File.Models.FileDetail()。 DTTT in the code is a datatable, FileDetial is a class in a Model which has Filename,Dt,receivedcount,status params.
代码中的DTTT是数据表,FileDetial是Model中的一个类,具有Filename,Dt,receivedcount,status参数。 I have checked all the Field datatypes and they are correct.
我检查了所有的字段数据类型,它们是正确的。 Not sure why I'm getting this error.
不知道为什么我得到这个错误。
return (from row in DTTT.AsEnumerable()
select new File.Models.FileDetail()
{
Filename = row.Field<string>("name"),
Dt = row.Field<DateTime?>("Dt"),
ReceivedCount = row.Field<int?>("count"),
status = row.Field<string>("status")
}
).ToList();
You should be able to cast it as a TimeSpan
: 您应该可以将其
TimeSpan
为TimeSpan
:
Dt = row.Field<TimeSpan>("Dt");
OR 要么
Dt = (DateTime)(row.Field<DateTime>("Dt") == DBNull.Value ? DateTime.MinValue :row.Field<DateTime>("Dt"));
Dbtype of my "DT" field is char and the ReceivedCount property field is declared as Datetime in my model. 我的“ DT”字段的Dbtype是char,在我的模型中,ReceivedCount属性字段声明为Datetime。 So there was a invalid cast exception.
因此,存在无效的强制转换异常。 changing my model field datatype to string worked.
将我的模型字段数据类型更改为字符串有效。 Thanks everyone.
感谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.