0

I want to create new fields and replace others when I map objects in C #, as I show below

public class one 
{
   public int a {get; set;}
   public int b {get; set;}
   public int c {get; set;}
}

public class two
{
   public int sum {get; set;} //sum = a + b +c ;
}

Mapper.Initialize(cfg =>
{
    cfg.CreateMap<one, two>();//?????mapping sum = a+b+c;
});

Please, any idea?

  • Having automapper do this kind of logic is a very suspect approach, magic mutations are never a good thing. – TheGeneral Jun 11 '19 at 23:06

1 Answers1

1

Use an Inline Mapping;

(PS your classes and properties should start with Uppercase)

public class One 
{
  public int A {get; set;}
  public int B {get; set;}
  public int C {get; set;}
}

public class Two
{
  public int Sum {get; set;} //sum = a + b +c ;
}

cfg.CreateMap<One, Two>()
  .ForMember(dest => dest.Sum, m => m.MapFrom(src => src.A + src.B + src.C));

DotNetFiddle Example

Erik Philips
  • 53,428
  • 11
  • 128
  • 150