相关疑难解决方法(0)

Automapper:如何映射嵌套对象?

我正在努力使用Automapper语法.我有一个PropertySurveys列表,每个包含1个属性.我希望将集合中的每个项目映射到一个新的对象,该对象组合了两个类.

所以我的代码看起来像;

            var propertySurveys = new List<PropertyToSurveyOutput >();
            foreach (var item in items)
            {
                Mapper.CreateMap<Property, PropertyToSurveyOutput >();
                var property = Mapper.Map<PropertyToSurvey>(item.Property);
                Mapper.CreateMap<PropertySurvey, PropertyToSurveyOutput >();
                property = Mapper.Map<PropertyToSurvey>(item);
                propertySurveys.Add(property);
            }
Run Code Online (Sandbox Code Playgroud)

我的简化课程看起来像;

public class Property
{
    public string PropertyName { get; set; }
}

public class PropertySurvey
{
    public string PropertySurveyName { get; set; }
    public Property Property { get; set;}
}

public class PropertyToSurveyOutput
{
    public string PropertyName { get; set; }
    public string PropertySurveyName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

因此,在PropertyToSurveyOutput对象中,在设置第一个映射PropertyName之后.然后在设置第二个映射PropertySurveyName之后,将PropertyName重写为null.我该如何解决?

c# automapper

5
推荐指数
1
解决办法
1万
查看次数

标签 统计

automapper ×1

c# ×1