Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to change</p> <pre class="lang-cs prettyprint-override"><code>.ForMember(dest =&gt; dest.ShirtColor, dest =&gt; dest.Condition(src =&gt; src.ShirtColor != null)) </code></pre> <p>to</p> <pre class="lang-cs prettyprint-override"><code>.ForMember(dest =&gt; dest.ShirtColor, opt =&gt; opt.Condition(src =&gt; !src.IsSourceValueNull)) </code></pre> <p>Note that you also don't need the following as they are mapped by convention:</p> <pre class="lang-cs prettyprint-override"><code>.ForMember(dest =&gt; dest.FirstName, opt =&gt; opt.MapFrom(src =&gt; src.FirstName)) .ForMember(dest =&gt; dest.LastName, opt =&gt; opt.MapFrom(src =&gt; src.LastName)) </code></pre> <hr> <p><strong>UPDATE - 2</strong></p> <p>The problem is related to the fact that the condition and mapping haven't been combined. I've created a simple extension method to allow for it.</p> <p>Here is a test to demonstrate it</p> <p><strong>Classes</strong></p> <pre class="lang-cs prettyprint-override"><code>public class Person { public string FirstName { get; set; } public string LastName { get; set; } } public class PersonA : Person { public int? ShirtColor { get; set; } } public class PersonB : Person { public OptionSetValue ShirtColor { get; set; } } public class OptionSetValue { public int OptionSet { get; set; } } </code></pre> <p><strong>MappingConfiguration</strong></p> <pre class="lang-cs prettyprint-override"><code>public class MyProfile : Profile { protected override void Configure() { Mapper.CreateMap&lt;PersonA, PersonB&gt;() .ForMember(dest =&gt; dest.ShirtColor, opt =&gt; opt.ConditionallyMapFrom( src =&gt; new OptionSetValue { OptionSet = src.ShirtColor.Value }, src =&gt; src.ShirtColor.HasValue)); } } public static class AutoMapperExtensions { public static void ConditionallyMapFrom&lt;TSource, TMember&gt;( this IMemberConfigurationExpression&lt;TSource&gt; expression, Expression&lt;Func&lt;TSource, TMember&gt;&gt; sourceMember, Func&lt;TSource, bool&gt; condition) { expression.Condition(condition); expression.MapFrom(sourceMember); } } </code></pre> <p><strong>UnitTests</strong></p> <pre class="lang-cs prettyprint-override"><code>[TestFixture] public class MappingTests { [Test] public void AutoMapper_Configuration_IsValid() { Mapper.Initialize(m =&gt; m.AddProfile&lt;MyProfile&gt;()); Mapper.AssertConfigurationIsValid(); } [Test] public void AutoMapper_ClientMapping_IsValid() { Mapper.Initialize(m =&gt; m.AddProfile&lt;MyProfile&gt;()); Mapper.AssertConfigurationIsValid(); var source = new PersonA { FirstName = "FirstA", LastName = "LastA", ShirtColor = null }; var destination = new PersonB { FirstName = "FirstB", LastName = "LastB" }; destination = Mapper.Map(source, destination); Assert.That(destination, Is.Not.Null); Assert.That(destination.FirstName, Is.EqualTo("FirstA")); Assert.That(destination.LastName, Is.EqualTo("LastA")); Assert.That(destination.ShirtColor, Is.Null); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload