Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an enum to int automatically via FastMember?

I'm using FastMember to convert a List<T> to a Datatable. Some classes contain enums and this is causing problems when passing the datatables as a TVP to a stored procedure.

public class MyObject
{
    public int Id {get; set;}
    public SomeEnum EnumHere {get; set;}
}

var dt = new DataTable();
using (var reader = ObjectReader.Create(myObjectsList))
{
    dt.Load(reader);
}

db.Execute<ResultObject>("insert_objects", new { dt }, commandType: CommandType.StoredProcedure);

FastMember converts the list, however the column for the enum has a DataType of SomeEnum. When passing the datatable as a TVP, the following exception is thrown:

Exception thrown: 'System.ArgumentException' in Dapper.dll

Additional information: The type of column 'SomeEnum' is not supported. The type is 'SomeEnum'

Is there a way to force FastMember to convert enums to int?

like image 367
Ivan-Mark Debono Avatar asked Oct 17 '22 19:10

Ivan-Mark Debono


1 Answers

You can create an anonymous type to with the right properties:

var newObjects = from m in myObjectList
                 select new { m.Id, EnumHere = (int)m.EnumHere };

var dt = new DataTable();
using (var reader = ObjectReader.Create(newObjects))
{
    dt.Load(reader);
}
like image 74
Tim P. Avatar answered Nov 01 '22 07:11

Tim P.