简体   繁体   中英

“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:

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.

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.

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.

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.

Thank you all.

Check if you are not comparing int with null column in database (ID) .

PS the passed recId is not null but values which it is checked against may be null in database table

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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