7

I can successfully map from IDataReader to a List of objects but when I want to take one DataRow it doesn't seem to work as expected.

Am I missing something simple here?

[TestFixture]
public class AutomapperTest
{
    [Test]
    public void TestMethod1()
    {
        DataTable dt = new DataTable("contact");
        dt.Columns.Add("FirstName");
        dt.Columns.Add("LastName");
        dt.Columns.Add("Line1");
        dt.Columns.Add("Line2");
        dt.Columns.Add("Line3");
        dt.Columns.Add("Suburb");
        dt.Columns.Add("State");
        dt.Columns.Add("Postcode");

        DataRow row = dt.NewRow();
        row.ItemArray = new [] { "Little", "Johnny", 
                                 "1 Random Place", "", "", 
                                 "Windsor", "Qld", "4030" };

        var dest = Mapper.DynamicMap<myObject>(row);

        Assert.AreEqual(row["FirstName"], "Little");
        Assert.IsNotNull(dest);
        Assert.AreEqual(dest.FirstName, "Little");
    }
}

Destination type:

public class myObject
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }
    public string Suburb { get; set; }
    public string State { get; set; }
    public string Postcode { get; set; }
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
Geoff
  • 210
  • 2
  • 3
  • 10

2 Answers2

11

You will have to implement your own custom value resolver

https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-resolvers

UPDATE

public class CustomResolver : IValueResolver
{
    public ResolutionResult Resolve(ResolutionResult source)
    {
        return source.New( Convert.ChangeType((source.Context.SourceValue as DataRow)[source.Context.MemberName], source.Context.DestinationType));
    }
}

and here is how to use it

Mapper.CreateMap<DataRow,myObject>().ForAllMembers(m=>m.ResolveUsing<CustomResolver>());
var dest = Mapper.Map<myObject>(row);

I recommend using Dapper. That way your data coming from database will be strongly typed or dynamic, and Automapper should be able to figure out the mappings.

CSV files can be read by Dapper through ODBC, I believe. But for CSV I'd recommend the LinqToCSV Nuget package instead.

Darek
  • 4,687
  • 31
  • 47
0

Another way is to use the ITypeConverter:

Create a custom TypeConverter class:

public class DataRowConverter : ITypeConverter<DataRow, Object>
{
    Object ITypeConverter<DataRow, Object>.Convert(DataRow source, Object destination, ResolutionContext context)
    {
        foreach (DataColumn col in source.Table.Columns)
        {
            var value = source[col].ToString();

            PropertyInfo prop = destination.GetType().GetProperty(
                col.ColumnName,
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.IgnoreCase);

            if (prop != null && prop.CanWrite)
            {
                prop.SetValue(destination, value, null);
            }
        }

        return destination;
    }
}

Then set the config:

cfg.CreateMap<DataRow, Object>().ConvertUsing<DataRowConverter>();

Finally, use the mapper as usual:

var results = new List<SearchResult>();
foreach (DataRow row in Res.data.Tables[0].Rows)
{
    var obj = mapper.Map<SearchResult>(row);
    results.Add(obj);
}

It should work with the latest version of AutoMapper.

ZooZ
  • 933
  • 1
  • 17
  • 25