How to configure Conditional Mapping in AutoMapper?

C#LambdaAutomapperAutomapper 2

C# Problem Overview


Suppose I have the following entities (classes)

public class Target
{
    public string Value;
}


public class Source
{
    public string Value1;
    public string Value2;
}

Now I want to configure Auto Map, to Map Value1 to Value if Value1 starts with "A", but otherwise I want to map Value2 to Value.

This is what I have so far:

Mapper
    .CreateMap<Source,Target>()
    .ForMember(t => t.Value, 
        o => 
            {
                o.Condition(s => 
                    s.Value1.StartsWith("A"));
                o.MapFrom(s => s.Value1);
                  <<***But then how do I supply the negative clause!?***>>
            })

However the part the still eludes me is how to tell AutoMapper to go take s.Value2 should the earlier condition fails.

It just seems to me the API was not designed as well as it could be... but may be it's my lack of knowledge getting in the way.

C# Solutions


Solution 1 - C#

Try this

 Mapper.CreateMap<Source, Target>()
        .ForMember(dest => dest.Value, 
                   opt => opt.MapFrom
                   (src => src.Value1.StartsWith("A") ? src.Value1 : src.Value2));

Condition option is used to add conditions to properties that must be met before that property will be mapped and MapFrom option is used to perform custom source/destination member mappings.

Solution 2 - C#

AutoMapper allows adding conditions to properties that must be met before that property will be mapped.

Mapper.CreateMap<Source,Target>()
      .ForMember(t => t.Value, opt => 
            {
                opt.PreCondition(s => s.Value1.StartsWith("A"));
                opt.MapFrom(s => s.Value1);
            })

Solution 3 - C#

With the conditional mapping you only can configure when the mapping should executed for the specified destination property.

So it means you can't define two mappings with different conditions for the same destination property.

If you have a condition like "if condition is true then use PropertyA else use PropertyB" then you should do it like "Tejal" wrote:

opt.MapFrom(src => src.Value1.StartsWith("A") ? src.Value1 : src.Value2)

Solution 4 - C#

AutoMapper allows you to add conditions to properties that must be met before that property will be mapped.

I was doing the mapping with some enum conditions, have a look that is little effort for the community from my side.

}

.ForMember(dest => dest.CurrentOrientationName, 
             opts => opts.MapFrom(src => src.IsLandscape? 
                                        PageSetupEditorOrientationViewModel.Orientation.Landscape : 
                                        PageSetupEditorOrientationViewModel.Orientation.Portrait));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlwynView Question on Stackoverflow
Solution 1 - C#TeeDeeView Answer on Stackoverflow
Solution 2 - C#BasimView Answer on Stackoverflow
Solution 3 - C#Marcus.DView Answer on Stackoverflow
Solution 4 - C#Khawaja AsimView Answer on Stackoverflow