title: Automapper: Convert an int to a string
body: <p>I am currently using AutoMapper in my c# MVC project and have come across an issue converting from a integer to a string.</p>
  
  <p>I have two classes:</p>
  
  <pre><code>public class Job
  {
      public string Prefix { get; set; }
      public int JobNumber { get; set; }
      public int Year { get; set }
      public int JobPriority { get; set; }
  
      public virtual EntityPriority EntityPriority { get; set; }
  }
  
  public class JobViewModel
  {
      public string JobNumberFull { get; set; }
  {
  </code></pre>
  
  <p>Using Automapper I would like to join all three properties in the Job class to form a string which will then be mapped to JobNumberFull in the JobViewModel class.:</p>
  
  <p>Prefix-JobNumber-Year</p>
  
  <p>To start I have kept things simple and not concatenated the fields into a string, I have just tried to take JobNumber (int) and map it to JobNumberFull (string) as follows:</p>
  
  <pre><code>Mapper.CreateMap&lt;int, string&gt;().ConvertUsing(Convert.ToString);
  Mapper.CreateMap&lt;Job, JobViewModel&gt;()
      .ForMember(d =&gt; d.JobPriorityId, opt =&gt; opt.MapFrom(s =&gt; s.EntityPriority.EntityPriorityID))
      .ForMember(d =&gt; d.JobPriorityLevel, opt =&gt; opt.MapFrom(s =&gt; s.EntityPriority.PriorityLevel))
      .ForMember(d =&gt; d.JobNumberFull, opt =&gt; opt.MapFrom(s =&gt; s.JobNumberYear));
  Mapper.AssertConfigurationIsValid();
  
  var jobsList = db.Jobs.Where(j =&gt; j.OperationID == operationId).Project().To&lt;JobViewModel&gt;().ToList();
  </code></pre>
  
  <p>When I run this I get the following error</p>
  
  <pre><code>Type 'System.String' does not have a default constructor
  </code></pre>
  
  <p>I know this is to do with</p>
  
  <pre><code>Convert.ToString
  </code></pre>
  
  <p>but I'm not sure what to do about it! I can do Convert.ToInt32 when going from a string to an int and it works like a treat. Do I need to create a custom type converter?</p>
  
  <p>How would I go about getting the string prefix-jobnumber-year?</p>
  
  <p>All help much appreciated.</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Thanks to the help of Jeremy and the AutoMapper website I have managed to get this working here is my solution, please feel free to comment if you think there is a more elegant solution:</p>
  
  <pre><code>public class Job 
  {
      public string Prefix { get; set; }
      public int JobNumber { get; set; }
      public int Year { get; set; }
      public int JobPriority { get; set; }
  
      public virtual EntityPriority EntityPriority { get; set; }
  }
  
  public class JobViewModel 
  {
      public int JobNumber { get; set; }
      public int Year { get; set; }
      public string JobNumberFull { get; set; }
  }
  
  public enum EntityPriority 
  {
    Normal = 0,
    High
  }
  
  public interface IValueResolver
  {
     ResolutionResult Resolve(ResolutionResult source);
  }
  
  public class JobNumberConverter : ValueResolver&lt;Job, string&gt;
  {
      protected override string ResolveCore(Job source)
      {
          return string.Format("{0}-{1}-{2}", source.Prefix, source.JobNumber, source.Year);
      }
  }
  
  public ActionResult Index()
  {
      Mapper.CreateMap&lt;Job, JobViewModel&gt;()
          .ForMember(jvm =&gt; jvm.JobNumberFull, opt =&gt; opt.ResolveUsing&lt;JobNumberConverter&gt;());
      Mapper.AssertConfigurationIsValid();
  
      List&lt;Job&gt; jobs = new List&lt;Job&gt;();
  
      Job j = new Job()
      {
          JobNumber = 1,
          Prefix = "prefix",
          Year = 2013,
          EntityPriority = EntityPriority.High,
          JobPriority = 2
      };
  
      Job j2 = new Job()
      {
          JobNumber = 2,
          Prefix = "prefix",
          Year = 2013,
          EntityPriority = EntityPriority.High,
          JobPriority = 2
      };
  
      Job j3 = new Job()
      {
          JobNumber = 3,
          Prefix = "prefix",
          Year = 2013,
          EntityPriority = EntityPriority.High,
          JobPriority = 2
      };
  
      jobs.Add(j);
      jobs.Add(j2);
      jobs.Add(j3);
  
      var jobViewModels = jobs.Select(job =&gt; Mapper.Map&lt;JobViewModel&gt;(job));
  
      return View();
  
  }
  </code></pre>