Home > Net >  AutoMapper - Mapping to Nested Destination List
AutoMapper - Mapping to Nested Destination List

Time:02-10

I am new to AutoMapper and I'm looking to map a Nested List to my Parent DTO, here is my DTO:

    public class ParentDTO
    {
        //Other Members
        public List<HostInfo> HostInfo { get; set; }
    }

    public class HostInfo
    {
        //Other Members
        public List<PluginInfo> Plugins { get; set; }
    }

    public class PluginInfo
    {
        //More Members
    }

Here is my Profile:

 public ParentProfile()
        {
           //Other Mappings

            CreateMap<Source, ParentDTO>()
            .ForMember(dto => dto.HostInfo, opt => opt.MapFrom(x => Source.Select(x => x.Data))))

            .ForPath(dto => dto.HostInfo.Select(x=> x.Plugins), opt => opt.MapFrom(x => x.Source.Select(x => x.Plugin)))
        }

However the following line:

            .ForPath(dto => dto.HostInfo.Select(x=> x.Plugins), opt => opt.MapFrom(x => x.Source.Select(x => x.Plugin)))

Creates this error:

System.ArgumentOutOfRangeException: 'Only member accesses are allowed. dto => dto.HostInfo.Select(x => x.Plugins) Arg_ParamName_Name

Here is my desired output:

#ParentDTO
{
  //other data,
  "hostInfo": [
    {
      //Other Data
      "Plugin": [
      {
        //Other Data
      },
      {
        //Other Data
      } 
      ]
    }
 ]
}

Here is my current output when removing the troubled code stated above:

#ParentDTO
{
  //other data,
  "hostInfo": [
    {
      //Other Data
      "Plugin": Null
 ]
}

CodePudding user response:

  •  Tags:  
  • Related