Search code examples
c#dapper

Convert an OracleRefCursor to a List of custom C# Objects


I have a Dapper statement that returns an OracleRefCursor:

try
{
    using (IDbConnection connection = Connection)
    {
        var parameters = new OracleDynamicParameters();
        parameters.Add("refCursorReturn", null, OracleMappingType.RefCursor, ParameterDirection.Output);
        IEnumerable<OracleRefCursor> temp = connection.Query<OracleRefCursor>("MySchema.MyStoredProc", parameters, commandType: CommandType.StoredProcedure);

        //I need to convert the OracleRefCursor to an IEnumerable or List of MyObject

        return IEnumerable<MyObject>;
    }
}
catch (Exception e)
{
    Console.WriteLine(e);
    throw;
}

I can't find any documentation online about how to convert an OracleRefCursor to anything I can use.


Solution

  • One way is (if ok with NOT using strong type like OracleRefCursor), then can use:

     IEnumerable<dynamic> temp = null;
    
     temp = connection.Query<dynamic>("MySchema.MyStoredProc", parameters, commandType: CommandType.StoredProcedure);
    
    foreach (var dataRow in temp)
    {
         var fields = dataRow as IDictionary<string, object>;
         // do something with fields. (store in datatable, list etc).
    }