0

I have models as below.

public class MainModel
{
    public object1 Property1{get; set;}
    public object2 Property2{get; set;}
    public object3 Property3{get; set;}
}

Public class object1{
    /// few properties;
}

Public class object2{
    /// few properties;
}

Public class object3{
    /// few properties;
}

Now how can I map these multiple objects to MainModel using AutoMapper?

Yannick Meeus
  • 5,643
  • 1
  • 35
  • 34
swathi
  • 119
  • 2
  • 11
  • possible duplicate of [Automapper convert from multiple sources](http://stackoverflow.com/questions/21413273/automapper-convert-from-multiple-sources) – PatrickSteele Feb 26 '15 at 12:49
  • It is similar Patrick, but the above solution will not work in my case, since I need to map object1, object2 and object3 to MainModel. In the given case, they are mapping two objects which has properties which is combined in the main model properties. – swathi Feb 26 '15 at 15:06
  • I don't see why the other answer wouldn't work. Could you give a concrete example with real source object (or objects) along with the real destination object that you can't get working? That's the best way to get help. – PatrickSteele Feb 26 '15 at 17:11

1 Answers1

0
Mapper.CreateMap<object1, MainModel>()
                        .ForMember(x => x.Property1, y => y.MapFrom(src => src));
Mapper.CreateMap<object2, MainModel>()
                      .ForMember(x => x.Property2, y => y.MapFrom(src => src));
Mapper..CreateMap<object3, MainModel>()
                   .ForMember(x => x.Property3, y => y.MapFrom(src => src));


object1 obj1 = new object1();
object2 obj2 = new object2();
object3 obj3 = new object3();

MainModel mm = AutoMapper.Mapper.Map<MainModel>(obj1);
mm = AutoMapper.Mapper.Map(obj2, mm);
mm = AutoMapper.Mapper.Map(obj3, mm);   
ncfuncion
  • 227
  • 1
  • 3
  • 11