我在以下代码中收到InvalidCastException错误,其中linq查询运行...
System.InvalidCastException was caught
HResult=-2147467262
Message=Specified cast is not valid.
Source=System.Data.Linq
StackTrace:
at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.Table`1.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
at PINProcess.Program.readCompoundFile(String logFile, String ConnectionString, FileInfo fInfo, ArrayList errors, Int32& putOnHold, Int32 TOId, String FeedFilename, String fileExtension, ArrayList NotFoundPINs) in c:\Users\Admin\Desktop\PINProcess\Program.cs:line 110
InnerException:
...代码
string PIN = line.Substring(8, 17); // PIN Number
string InDateTime = line.Substring(25, 14); //Date for In/Out from system
int vehicleMasterID = 0;
try
{
DataContext dc = new DataContext(ConnectionString);
//get MasterId from PIN number
var q = dc.Masters.SingleOrDefault(v => v.Dead == false && v.PINNo.ToUpper().Trim() == PIN.ToUpper().Trim());
MasterID = q.Id;
etc.....
因此,据我所知,linq查询中出现错误。我不明白为什么它有问题,因为值可以找到,这适用于前3个记录然后命中InvalidCastException。 PIN
是一个字符串,db中的PINNo
是nvarchar(20)所以我不确定错误来自哪里。 PIN
的长度永远不会超过20,并且总是非常类似于那些有效并且不会绘制错误的长度。
有没有人见过这个?