views:

33

answers:

2

I am getting a cast error with Linq to a DataTable that was populated with Oracle.

bool isTrue = DataTable.AsEnumerable().Any
      (x => x.Field<int>("MYNUMBERFIELD") == MYNUMBER);

In SQL this works fine as expected. In Oracle is fails with a cast error on the cast. In C# code the same thing happens when you do the following:

int myint = (int)VariableRetrievedFromOracleDB;

If you change it to int myint = Convert.ToInt32(VariableRetrievedFromOracleDB) it works fine.

Any ideas how to handle this with the lambda?

+2  A: 
bool isTrue = 
    DataTable.AsEnumerable()
             .Any(x => Convert.ToInt32(x.Field("MYNUMBERFIELD")) == MYNUMBER);
Justin Niessner
That is what I thought at first too - but that still fails.
tsells
@tsells - Wait. Did MYNUMBER come from Oracle or is it defined in your code?
Justin Niessner
The underlying business object was populated from Oracle so yes it required a cast. I modified the code as Linq had some issues here in general.
tsells
A: 

If the oracle column is a number, there's a chance that what you get is a decimal or double Try to cast using the type returned by:

DataTable.Columns["MYNUMBERFIELD"].DataType

vc 74