有没有一种方法可以映射具有多个列表类的父根类(包括使用Automapper的子类)?

时间:2019-04-17 18:01:36

标签: c# automapper

我能够创建成功的映射,但是只有一个父根类的列表类被映射。在下面的代码中,只有List Designation类失败,而List Customfield类失败。

映射:`

  cfg.CreateMap<Emp,Designation>()
  .ForMember(a => a.desigs, b => b.ResolveUsing(c => c.designations));

  cfg.CreateMap<Emp, Employment>()
   .ForMember(a => a.role, b => b.ResolveUsing(c => c..firstname));

  cfg.CreateMap<RootObjectSource, RootObjectDest>()
   .ForMember(a => a.Customfield, b => b.ResolveUsing(c => c.Emp));

  cfg.CreateMap<Emp, Customfield>()
   .ForMember(x => x.Customfield, y => y.MapFrom(x => x.Id));

源类:

 public class RootObjectSource
    {
        public List<Emp> employments{ get; set; }
    }

 public class Emp
    {
        public string Id{get; set;}
        public string Firstname{get; set;}
        public string Lastname{get; set;}
        public List<Desig> Desigs {get; set;}
    }

public class Desig
    {             
         public string role{ get; set; }
         public string salary{ get; set; }
    }

 Destination classes:

     public class RootObjectDest
      {
       public string principal{ get; set; }
       public List<Designation> designations{ get; set; }
       public List<Customfield> Customfield { get; set; }
      }

  public class Designation
     {             
              public List<Employment> employment { get; set; }
              public string role{ get; set; }
              public string salary{ get; set; }
     }
   public class Employment
    {
        public string Firstname{ get; set; }
        public string Lastname{ get; set; }
    }  
    public class Customfield
    { 
      public string stringfield{ get; set; }

    } 

我们需要映射目标类别的所有列表类别

0 个答案:

没有答案