title: Automapper: Convert string to datetime from dataset
body: <p>I use automapper to map a dataset to an IEnumerable of an object i created.</p>
  
  <p>I have a dataset that looks like (testcode):</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          Mapper.CreateMap&lt;string, DateTimeOffset&gt;().ConvertUsing&lt;StringToDateTimeOffsetConverter&gt;();
          Mapper.CreateMap&lt;string, DateTime&gt;().ConvertUsing&lt;StringToDateTimeConverter&gt;();
          Mapper.CreateMap&lt;IDataReader, SentMessageListItem&gt;()
                .ForMember(dest =&gt; dest.SentDate, opt =&gt; opt.MapFrom(src =&gt; src.GetString(2)))
                .ForMember(dest =&gt; dest.DeleteDate, opt =&gt; opt.MapFrom(src =&gt; src.GetString(3)));
  
          var sentmessages = Mapper.Map&lt;IDataReader, IEnumerable&lt;SentMessageListItem&gt;&gt;(Model.DataSet.CreateDataReader());
      }
  }
  
  public class SentMessageListItem
  {
      public virtual int Id { get; set; }
      public virtual string Subject { get; set; }
      public virtual DateTimeOffset SentDate { get; set; }
      public virtual DateTime DeleteDate { get; set; }
  }
  
  internal class StringToDateTimeOffsetConverter : ITypeConverter&lt;string, DateTimeOffset&gt;
  {
      public DateTimeOffset Convert(ResolutionContext context)
      {
          var objDateTime = context.SourceValue;
          DateTimeOffset dateTime;
  
          if (objDateTime == null)
          {
              return default(DateTimeOffset);
          }
  
          if (DateTimeOffset.TryParse(objDateTime.ToString(), out dateTime))
          {
              return dateTime;
          }
  
          return default(DateTimeOffset);
      }
  }
  
  internal class StringToDateTimeConverter : ITypeConverter&lt;string, DateTime&gt;
  {
      public DateTime Convert(ResolutionContext context)
      {
          var objDateTime = context.SourceValue;
          DateTime dateTime;
  
          if (objDateTime == null)
          {
              return default(DateTime);
          }
  
          if (DateTime.TryParse(objDateTime.ToString(), out dateTime))
          {
              return dateTime;
          }
  
          return default(DateTime);
      }
  }
  
  public static class Model
  {
      public static readonly DataSet DataSet = GetDataSet();
  
      private static DataSet GetDataSet()
      {
          var set = new DataSet();
          var table1 = new DataTable();
          table1.Columns.Add("id");
          table1.Columns.Add("subject");
          table1.Columns.Add("sentdate");
          table1.Columns.Add("deletedate");
          table1.Rows.Add("0", "Subject", "01/01/2013", "01/01/2013");
          set.Tables.Add(table1);
  
          return set;
      }
  }
  </code></pre>
  
  <p>error:</p>
  
  <pre><code>Mapping types:
  IDataReader -&gt; IEnumerable`1
  System.Data.IDataReader -&gt; System.Collections.Generic.IEnumerable`1[[CNH.CSCN.BBS.Entities.Domain.Entities.SentMessageListItem, CNH.CSCN.BBS.Entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
  
  Destination path:
  IEnumerable`1
  
  Source value:
  System.Data.DataTableReader
  </code></pre>
  
  <p>What am i doing wrong?</p>
  
  <p>The error is caused by the datetimeoffset and datetime, because if i remove them, automapper has no problem with mapping my dataset to my object.</p>