First of all, Automapper supports collection mapping. You do not need to display each element in a loop.
- , . ( ).
- Automapper , :
Mapper.CreateMap<PropertySurvey, PropertyToSurveyOutput>()
.ForMember(pts => pts.PropertyName, opt => opt.MapFrom(ps => ps.Property.PropertyName));
:
var items = new List<PropertySurvey>
{
new PropertySurvey {
PropertySurveyName = "Foo",
Property = new Property { PropertyName = "X" } },
new PropertySurvey {
PropertySurveyName = "Bar",
Property = new Property { PropertyName = "Y" } }
};
var propertySurveys = Mapper.Map<List<PropertyToSurveyOutput>>(items);
:
[
{
"PropertyName": "X",
"PropertySurveyName": "Foo"
},
{
"PropertyName": "Y",
"PropertySurveyName": "Bar"
}
]
UPDATE: Property , - Property:
Mapper.CreateMap<Property, PropertyToSurveyOutput>();
PropertySurvey. PropertySurvey:
Mapper.CreateMap<PropertySurvey, PropertyToSurveyOutput>()
.AfterMap((ps, pst) => Mapper.Map(ps.Property, pst));