7

I have two sub class that I need to copy a List element from into a master object

public Class Foo1 : Anote
{
  public bool Ison { get; set;}
  public List<Anote>Anotes { get; private set;}

  public Foo1()
  {
    this.Anotes = new List<Anote>();
  }
}

public Class Foo2 : Bnote
{
  public bool Ison { get; set;}
  public List<Bnote>Anotes { get; private set;}

  public Foo2()
  {
    this.Anotes = new List<Bnote>();
  }
}

public Class Foo3 : Cnote
{
   public bool Ison { get; set;}
   public List<Cnote>Anotes { get; private set;}
   public List<Cnote>Bnotes { get; private set; }

}

I will be retreiving data from a database and putting this data into Foo1 and Foo2. I then need to Map the List data from Foo1 and Foo2 into the List elements in Foo3.

I have done

Foo1A foo1a = new Foo1A();
Foo2A foo2a = new Foo2A();

Mapper.CreateMap<Foo1, Foo1A>();
Mapper.CreateMap<Foo2, Foo2A>();
Mapper.Map<Foo1, Foo2A>(foo1, foo1a);
Mapper.Map<Foo2, Foo2A>(foo2, foo2a);

and this works but what I need to do is

Map the List Anotes in Foo1 to the List Anotes in Foo3 Map the List Anotes in Foo2 to the List Bnotes in Foo3.

Ben T
  • 4,656
  • 3
  • 22
  • 22

2 Answers2

12

You should just be able to do:

Mapper.CreateMap<ANote, CNote>();

Mapper.CreateMap<Foo1, Foo3>()
    .ForMember(dest => dest.ANotes, opt => opt.MapFrom(src => src.ANotes))
    .ForMember(dest => dest.BNotes, opt => opt.Ignore());

Mapper.CreateMap<Foo2, Foo3>()
    .ForMember(dest => dest.BNotes, opt => opt.MapFrom(src => src.ANotes))
    .ForMember(dest => dest.ANotes, opt => opt.Ignore());

Foo3 foo3 = new Foo3();

Mapper.Map<Foo1, Foo3>(foo, foo3);
Mapper.Map<Foo2, Foo3>(foo2, foo3);

foo3.ANotes and foo3.BNotes should both have been mapped correctly.

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • For anyone else that stumbles upon this, I have been trying to use this solution to merge two lists of objects. I haven't been able to figure out how to get that to work. I had to use a foreach and then map each item and manually add it to a list. – Icestorm0141 Apr 06 '15 at 15:56
  • @Icestorm0141: Do you have a new question open? – Andrew Whitaker Apr 06 '15 at 16:01
  • In second mapping Foo2 to Foo3, i.e., `.ForMember(dest => dest.BNotes, opt => opt.MapFrom(src => src.ANotes))` shouldn't this `be src,BNotes` instead of `src.ANotes`? – Janis S. Sep 13 '17 at 10:25
  • The problem I have with this solution is you're duplicating the mappings for Foo1 and Foo2. If you have complicated mappings, duplicating that many is a code smell. I was hoping Automapper had a way to do this: `Mapper.CreateMap();` Like the OP, Src1 and Src2 are almost identical in structure. – goku_da_master Apr 19 '19 at 18:52
  • In this example you are writing the lists into two different lists what if you wanted to write them all the to the same list in Foo3 ? [see](https://stackoverflow.com/q/67833797/1841839) – Linda Lawton - DaImTo Jun 04 '21 at 09:31
0

The accepted answer didn't feel like an acceptable solution for my situation. I was falling asleep last night and had this thought:

Lets say you want to map your two classes, Foo1 and Foo2 to Foo3

public Class Foo1 : Anote
{
  public bool Ison { get; set;}
  public List<Anote>Anotes { get; private set;}

  public Foo1()
  {
    this.Anotes = new List<Anote>();
  }
}

+

public Class Foo2 : Bnote
{
  public bool Ison { get; set;}
  public List<Bnote>Anotes { get; private set;}

  public Foo2()
  {
    this.Anotes = new List<Bnote>();
  }
}

=

public Class Foo3 : Cnote
{
   public bool Ison { get; set;}
   public List<Cnote>Anotes { get; private set;}
   public List<Cnote>Bnotes { get; private set; }

}

All you really need is another wrapper class for Automapper purposes.

public class Foo1And2 {
    public Foo1 Foo1 {get;set;}
    public Foo2 Foo2 {get;set;}
}

And then define the mapping:

CreateMap<Foo1And2, Foo3>().ConvertUsing(x => ConvertFoo1And2ToFoo3(x));
public Foo3 ConvertFoo1And2ToFoo3(Foo1And2 foo1And2)
{
    return new Foo3
    {
        Ison = foo1And2.Foo1.Ison && foo1And2.Foo2.Ison,
        Anotes = foo1And2.Foo1.Anotes,
        Bnotes = foo1And2.Foo2.Bnotes,
    };
}

And use it

var foo3 = Map<Foo3>(new Foo1And2
{
    Foo1 = foo1,
    Foo2 = foo2,
});