Home > Net >  Cast array returned from Oracle stored procedure
Cast array returned from Oracle stored procedure

Time:12-16

I am having difficulty reading values from the following object type array returned from Oracle stored procedure

var arrUtil = cmd.Parameters["UTILBYDAY"].Value;

The arrUtil array is of the below type and has these values. Any idea how to cast it to double type in c#?

{Oracle.ManagedDataAccess.Types.OracleDecimal[10]}
    [0]: {28.16901408450704225352112676056338028169}
    [1]: {57.24381625441696113074204946996466431095}
    [2]: {29.34782608695652173913043478260869565217}
    [3]: {22.02166064981949458483754512635379061372}
    [4]: {70.56737588652482269503546099290780141844}
    [5]: {79.78723404255319148936170212765957446809}
    [6]: {62.41134751773049645390070921985815602837}
    [7]: {23.40425531914893617021276595744680851064}
    [8]: {5.31914893617021276595744680851063829787}
    [9]: {63.507109004739336492890995260663507109}

I have tried to cast it to double using

double[] arraUtil = (double[]) cmd.Parameters["UTILBYDAY"].Value;

but it ended up in error that said unable to cast Oracle.ManagedDataAccess.Types.OracleDecimal[] to double[].

I have also tried googled around but couldn't find anything helpful. Any help is appreciated. Thank you.

CodePudding user response:

Oracle.ManagedDataAccess.Types.OracleDecimal type defines an explicit cast operator to convert its value to double type.

You can convert them like this:

using System.Linq;
using Oracle.ManagedDataAccess.Types;

OracleDecimal[] arrUtil = cmd.Parameters["UTILBYDAY"].Value;

// Convert to double
double[] utils = arrUtil.Select(util => (double)util).ToArray();

// Convert to decimal
decimal[] utils = arrUtil.Select(util => (decimal)util).ToArray();

// Please note: This does NOT work!
double[] utils = arrUtil.Cast<double>().ToArray();

You can cast OracleDecimal to byte, short, int, long, float, double and decimal.

Here you can find all the explicit cast operators.

  • Related