title: C# AutoMapper Conditional Mapping based upon target value
body: <p>Please can any one advise how to use conditional mapping in AutoMapper to map a value in the TARGET object from a SOURCE object based upon an existing TARGET property value?</p>
  
  <p>So my source class is:</p>
  
  <pre><code>public class UserDetails
  {
      public String Nickname { get; set; }
  }
  </code></pre>
  
  <p>My target class is:</p>
  
  <pre><code>public class ProfileViewModel
  {
      public Boolean NicknameIsVisible { get; set;
      public String Nickname { get; set; }
  }
  </code></pre>
  
  <p>I want to set the "Nickname" property value in the TARGET to match the "Nickname" property value in the SOURCE only if the target property "NicknameIsVisible" value is already set to TRUE, otherwise I want to set the TARGET "Nickname" property value to an empty string.</p>
  
  <p>I was trying something like this (which wont compile )...</p>
  
  <pre><code>Mapper.CreateMap&lt;UserDetails, ProfileViewModel&gt;()
  .ForMember(
              destination =&gt; destination.Nickname,
              option =&gt; option.
                  .MapFrom(
                      source =&gt; source.NicknameIsVisible ? 
                      source.Nickname :
                      String.Empty)
  );
  </code></pre>
  
  <p>but "NicknameIsVisible" is not a property of my SOURCE but of my TARGET. </p>
  
  <p>BTW, My ProfileViewModel is bound to three entities using Owain Wragg's method (<a href="http://consultingblogs.emc.com/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx">http://consultingblogs.emc.com/owainwragg/archive/2010/12/22/automapper-mapping-from-multiple-objects.aspx</a>) and it is another entity that gives the value to the "NicknameIsVisible" property.</p>
  
  <p>Please can anyone suggest the right syntax to use for this problem?</p>