-1

I am using ASP.NET Web API and C#. As i am new to Express Mapper and i am having ADO.NET code which is returning list of results.How to map using Express Mapper?

Sai Kumar
  • 31
  • 2
  • 12

1 Answers1

1

This is a test to demonstrate how to work with custom mappers in ExpressMapper. I hope you'll be able to use it accordingly -

    public static void Main()
    {
        var ds = new DataSet();
        var dt = new DataTable();
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Age", typeof(int));
        dt.Rows.Add("Test", 10);
        dt.Rows.Add("Test2", 10);
        ds.Tables.Add(dt);

        var mapped = Mapper.Map<DataTable, List<RequestModel>>(ds.Tables[0], new CustomTypeMapper());

    }

where -

class RequestModel
{
    public int Age { get; set; }
    public string Name { get; set; }
}

class CustomTypeMapper : ICustomTypeMapper<DataTable, List<RequestModel>>
{
    public List<RequestModel> Map(IMappingContext<DataTable, List<RequestModel>> context)
    {
        if (context.Source == null)
            throw new ArgumentNullException();

        var output = new List<RequestModel>();
        foreach (DataRow row in context.Source.Rows)
        {
            output.Add(new RequestModel
            {
                Age = row.Field<int>("Age"),
                Name = row.Field<string>("Name")
            });
        }

        return output;
    }
}
Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24