简体   繁体   English

使用自动映射器映射嵌套列表

[英]Mapping nested lists with automapper

This is my class setup. 这是我的课程设置。 How do i map only Invalid=false for DTOReportObservation AND DTOReportObservationLocation items? 如何仅将Invalid = false映射到DTOReportObservation和DTOReportObservationLocation项目?

reports = Mapper.Map<List<Report>, List<DTOReport>>(userReports);

    public class DTOReport
    {
        public List<DTOReportObservation> Observations;
    }

        public class DTOReportObservation
    {
        public Guid ReportObservationID { get; set; }
        public Guid ReportID { get; set; }
        public bool Invalid { get; set; }

        public List<DTOReportObservationLocation> ObservationLocations;
    }

     public class DTOReportObservationLocation
    {
        public Guid ReportObservationLocationID { get; set; }
        public Guid ReportObservationID { get; set; }
        public bool Invalid { get; set; }
    }

 CreateMap<Report, DTOReport>(MemberList.Source)
                .ForMember(d => d.Observations, opt => opt.MapFrom(src => src.ReportObservations))
                //??ReportObservations.Locations

With automapper you shouldn't need to create maps of lists. 使用自动映射器,您无需创建列表映射。 You just create a map from one type to another and let automapper iterate over the collections. 您只需创建从一种类型到另一种类型的映射,然后让自动映射器遍历集合。

Can you also clarify what you mean by Invalid=false seeing as Invalid is a guid type. 您是否还可以澄清一下Invalid = false的含义,因为Invalid是一种GUID类型。

For mapping only when invalid is false you can use the conditional mapping. 对于仅当invalid为false时的映射,您可以使用条件映射。 https://automapper.readthedocs.io/en/latest/Conditional-mapping.html . https://automapper.readthedocs.io/en/latest/Conditional-mapping.html

For more info on lists see here in the docs about collections. 有关列表的更多信息,请参见文档中有关集合的信息。 https://automapper.readthedocs.io/en/latest/Lists-and-arrays.html https://automapper.readthedocs.io/en/latest/Lists-and-arrays.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM