60

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.

wonea
  • 4,783
  • 17
  • 86
  • 139
Alwyn
  • 8,079
  • 12
  • 59
  • 107
  • 3
    Just have a second `ForMember` call with the inverse of the condition in this one if you're really stuck and the answer provided by Tejal doesn't cut it. – Joe Apr 16 '14 at 15:06

4 Answers4

120

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.

bhupendra patel
  • 3,139
  • 1
  • 25
  • 29
TeeDee
  • 1,450
  • 1
  • 13
  • 15
  • 2
    Hmmm I actually need a way to access the target value too somehow - but still this answers the question asked. – Alwyn Jul 25 '13 at 00:40
  • What are you exactly trying to do? – TeeDee Jul 25 '13 at 04:53
  • The condition is a bit more complex than the one described, that depends on the source value only. I need to somehow get to the resolutioncontext and figure out the target value as well. – Alwyn Jul 25 '13 at 20:20
14

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);
            })
  • 5
    Hi and welcome to stackoverflow, and thank you for answering. While this code might answer the question, can you consider adding some explanation for what the problem was you solved, and how you solved it? This will help future readers to understand your answer better and learn from it. – Plutian Feb 18 '20 at 09:32
  • 2
    the best way is this answer – cura Jun 22 '21 at 15:00
  • 1
    Except it doesn't work if you're using ProjectTo – m12lrpv Aug 11 '22 at 00:06
4

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)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Marcus.D
  • 679
  • 6
  • 7
2

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));
hkutluay
  • 6,794
  • 2
  • 33
  • 53
Khawaja Asim
  • 1,327
  • 18
  • 38