title: AutoMapper and conditional mapping to null
body: <p>I want to create mapping like this:</p>
  
  <ul>
  <li><p>if the source property has it's Id == 0, set the destination property to null</p></li>
  <li><p>if the source property has it's Id != 0, map this property using default configuration</p></li>
  </ul>
  
  <p>For example:</p>
  
  <pre><code>Mapper.CreateMap&lt;ItemViewModel, Item&gt;()
      .ForMember(x =&gt; x.DestinationArticle, o =&gt; o.SetNullIfSourceHasIdZero(x =&gt; x.SourceArticle));
  </code></pre>
  
  <p>The DestinationArticle and SourceArticle properties are of implementing:</p>
  
  <pre><code>public interface IEntity
  {
      long Id { get; set }
  }
  </code></pre>
  
  <p>My current solution is this:</p>
  
  <pre><code>.ForMember(x =&gt; x.DestinationArticle, o =&gt; o.MapFrom(x =&gt; x.SourceArticle != null &amp;&amp; x.SourceArticle.Id != 0 ? Mapper.Map&lt;Article&gt;(x.SourceArticle) : null))
  </code></pre>
  
  <p>which is not so good and not generic.</p>
  
  <p>So, is there any way to create, mentioned before, SetNullIfSourceHasIdZero extension method or something, which could make my life easier?</p>