简体   繁体   English

System.InvalidCastException:指定的强制转换无效(linq查询)

[英]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 : 您应该可以将其TimeSpanTimeSpan

 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.

相关问题 Linq To数据集错误System.InvalidCastException:指定的强制转换无效 - Linq To Data set error System.InvalidCastException: Specified cast is not valid System.InvalidCastException:指定的强制转换无效。 -DynamoDB查询 - System.InvalidCastException: Specified cast is not valid. - DynamoDB Query 指定的转换无效-System.InvalidCastException - Specified cast is not valid - System.InvalidCastException System.InvalidCastException:指定的强制转换无效 - System.InvalidCastException: Specified cast is not valid “ System.InvalidCastException:指定的转换无效”-DateTime - “System.InvalidCastException: Specified cast is not valid” - DateTime System.InvalidCastException:指定的强制转换在.ExecuteScalar上无效 - System.InvalidCastException: Specified cast is not valid at .ExecuteScalar System.InvalidCastException:&#39;指定的强制转换无效。 - System.InvalidCastException: 'Specified cast is not valid.' 使用LINQ to SQL和sqlmetal的C#中的错误System.InvalidCastException:指定的强制转换无效 - Error in C# using LINQ to SQL and sqlmetal System.InvalidCastException: Specified cast is not valid System.InvalidCastException:“指定的演员表无效。” C# MYSQL - System.InvalidCastException: 'Specified cast is not valid.' C# MYSQL 错误:GetCharacters System.InvalidCastException:指定的强制类型转换无效的服务器 - Error: GetCharacters System.InvalidCastException: Specified cast is not valid server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM