JDS JDS - 1 month ago 8
C# Question

Unable to cast object of type 'Oracle.ManagedDataAccess.Types.OracleDecimal[]' to type 'System.IConvertible'

I am not sure how to resolve this error.

I have a class

public class MyClass
{
public OracleDecimal ID { get; set; }
public string FIELD1 { get; set; }
public string FIELD2 { get; set; }
public OracleDate FIELD3 { get; set; }
}


I built a List then make a call to the SP.

OracleParameter p_ID = new OracleParameter("p_ID", OracleDbType.Decimal, records.Select(x => x.ID).ToArray(), ParameterDirection.Input);
OracleParameter p_FIELD1 = new OracleParameter("p_FIELD1", OracleDbType.Varchar2, records.Select(x => x.FIELD1).ToArray(), ParameterDirection.Input);
OracleParameter p_FIELD2 = new OracleParameter("p_FIELD2", OracleDbType.Varchar2, records.Select(x => x.FIELD2).ToArray(), ParameterDirection.Input);
OracleParameter p_FIELD3 = new OracleParameter("p_FIELD3", OracleDbType.Date, records.Select(x => x.FIELD3).ToArray(), ParameterDirection.Input);

var result = context.Database.ExecuteSqlCommand("BEGIN MY_PACKAGE.MY_PROC(:p_ID, :p_FIELD1, :p_FIELD2, :p_FIELD3); END;", p_ID, p_FIELD1, p_FIELD2, p_FIELD3);


However I get the error
Unable to cast object of type 'Oracle.ManagedDataAccess.Types.OracleDecimal[]' to type 'System.IConvertible'.

I'm not sure how to resolve this error.

When I create my list I have the following:

OracleDate myDate = new OracleDate(DateTime.Now);
var myRecord = new MyClass
{
ID = ((OracleDecimal)ID).Value,
FIELD1 = field1,
FIELD2 = field2,
FIELD3 = myDate
};


Any suggestions would be appreciated.

Answer

records.Select(x => ...) will return a list of values, not a single value.

You should wrap your code around a foreach loop:

foreach(var record in records)
{
    OracleParameter p_ID = new OracleParameter("p_ID", OracleDbType.Decimal, record.Id, ParameterDirection.Input);
    OracleParameter p_FIELD1 = new OracleParameter("p_FIELD1", OracleDbType.Varchar2, record.FIELD1, ParameterDirection.Input);
    OracleParameter p_FIELD2 = new OracleParameter("p_FIELD2", OracleDbType.Varchar2, records.FIELD2, ParameterDirection.Input);
    OracleParameter p_FIELD3 = new OracleParameter("p_FIELD3", OracleDbType.Date, records.FIELD3, ParameterDirection.Input);

    var result = context.Database.ExecuteSqlCommand("BEGIN MY_PACKAGE.MY_PROC(:p_ID, :p_FIELD1, :p_FIELD2, :p_FIELD3); END;", p_ID, p_FIELD1, p_FIELD2, p_FIELD3);
}