简体   繁体   English

使用LINQ时,“输入字符串格式不正确”

[英]“Input string was not in a correct format” when using LINQ

I am trying to do a FirstOrDefault() query on an Object Set and a weird exception is thrown: 我试图对对象集执行FirstOrDefault()查询并抛出一个奇怪的异常:

My Code: 我的代码:

private RecordObject GetRecordByID(int recID)
{
    return _objectSet.FirstOrDefault(rec => rec.ID == recID);
}

Exception Message: 例外消息:

Input string was not in a correct format 输入字符串的格式不正确

InnerException is null. InnerException为null。

Stack Trace: 堆栈跟踪:

at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer&         number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Convert.ToDecimal(String value, IFormatProvider provider)
at System.String.System.IConvertible.ToDecimal(IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at Oracle.DataAccess.Client.OracleDataReader.ChangeType(Object sourceValue, Type targetType)
at Oracle.DataAccess.Client.OracleDataReader.GetValue(Int32 i)
at System.Data.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
at System.Data.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
at lambda_method(Closure , Shaper )
at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
at System.Linq.SystemCore_EnumerableDebugView`1.get_Items()

The weird thing is that I'm not doing any type conversions. 奇怪的是,我没有进行任何类型的转换。

PS: The database is Oracle 10g, but since I'm using EF, I guess this does not matter but just in case you need this detail. PS:数据库是Oracle 10g,但由于我使用EF,我想这无所谓,但万一你需要这个细节。

Ok, I've sorted this out and I will keep this question in case someone has the same problem. 好的,我已经对此进行了整理,如果有人遇到同样的问题,我会保留这个问题。

To solve it, I needed to re-import my tables from the database, apparently, there was some incompatible column(s) between my ADO.Net objects and the database. 为了解决这个问题,我需要从数据库重新导入表格,显然,我的ADO.Net对象和数据库之间存在一些不兼容的列。

I think, EF was trying to make a Cast operation to compensate for my mistake (leaving non-compatible versions), and the cast is not possible, and this is where the type-conversion came from. 我认为,EF试图进行Cast操作以弥补我的错误(留下不兼容的版本),并且无法进行强制转换,这就是类型转换的来源。

Thank you all. 谢谢你们。

Check if you are not comparing int with null column in database (ID) . 检查是否要将int与数据库(ID)中的空列进行比较。

PS the passed recId is not null but values which it is checked against may be null in database table PS传递的recId不为null,但在数据库表中检查的值可能为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM