title: Automapper conditional map from multiple source fields
body: <p>I've got a source class like the following:</p>
  
  <pre><code>public class Source
  {
      public Field[] Fields { get; set; }
      public Result[] Results { get; set; }
  }
  </code></pre>
  
  <p>And have a destination class like:</p>
  
  <pre><code>public class Destination
  {
      public Value[] Values { get; set; }
  }
  </code></pre>
  
  <p>So I want to map from EITHER <code>Fields</code> or <code>Results</code> to <code>Values</code> depending on which one is not null (only one will ever have a value). </p>
  
  <p>I tried the following map:</p>
  
  <pre><code>CreateMap&lt;Fields, Values&gt;();                
  CreateMap&lt;Results, Values&gt;();                
  
  CreateMap&lt;Source, Destination&gt;()                
              .ForMember(d =&gt; d.Values, opt =&gt;
              {
                  opt.PreCondition(s =&gt; s.Fields != null);
                  opt.MapFrom(s =&gt; s.Fields });
              })
              .ForMember(d =&gt; d.Values, opt =&gt;
              {
                  opt.PreCondition(s =&gt; s.Results != null);
                  opt.MapFrom(s =&gt; s.Results);
              });
  </code></pre>
  
  <p>Only issue with this is that it looks if the last <code>.ForMember</code> map doesn't meet the condition it wipes out the mapping result from the first map.</p>
  
  <p>I also thought about doing it as a conditional operator:</p>
  
  <p><code>opt =&gt; opt.MapFrom(s =&gt; s.Fields != null ? s.Fields : s.Results)</code></p>
  
  <p>But obviously they are different types so don't compile.</p>
  
  <p>How can I map to a single property from source properties of different types based on a condition?</p>
  
  <p>Thanks</p>