简体   繁体   中英

Why Automapper returns null while mapping List<T> in aspnet.core 3.1?

In my .net core 3.1 I would like to show nested elements using automapper.

In my dto I have field:

    public class ServerChartDto
    {
       .. other values
        public DiskSizeInfo DiskSize { get; set; }
    }

    public class DiskSizeInfo
    {
        public List<object> Projects { get; set; }
        public long Total { get; set; }
    }

My mapper looks like:

CreateMap<Organization, ServerChartDto>()
   ...other mappings
 .ForMember(d => d.DiskSize, o => o.MapFrom(s => s.Projects
                    .Select(y => new DiskSizeInfo
                    {
                        Projects =
                        {
                            new
                            {
                                Name = y.Name,
                                Used = y.Servers.Sum(z => z.DiskSize)
                            }
                        },
                        Total = y.Servers.Sum(z => z.DiskSize)
                    }).ToList()))

Want result something like:

diskSize: {
    projects: [
      {
        name: 'fewregrge'
        used: 234
      }
    ],
    total: 2342
  }

you are returning incompatible types:

public class ServerChartDto
{
   .. other values
    public DiskSizeInfo DiskSize { get; set; }
}

public class DiskSizeInfo
{
    public List<object> Projects { get; set; }
    public long Total { get; set; }
}

CreateMap<Organization, ServerChartDto>()
   ...other mappings
 .ForMember(d => d.DiskSize, o => o.MapFrom(s => s.Projects
                    .Select(y => new DiskSizeInfo
                    {
                        Projects =
                        {
                            new
                            {
                                Name = y.Name,
                                Used = y.Servers.Sum(z => z.DiskSize)
                            }
                        },
                        Total = y.Servers.Sum(z => z.DiskSize)
                    }).ToList()))

ServerChartDto.DiskSize has type DiskSizeInfo

ForMember(d => d.DiskSize, o => o.MapFrom(s => s.Projects
                .Select(y => new DiskSizeInfo
                {
                    Projects =
                    {
                        new
                        {
                            Name = y.Name,
                            Used = y.Servers.Sum(z => z.DiskSize)
                        }
                    },
                    Total = y.Servers.Sum(z => z.DiskSize)
                }).ToList()))

in the previous code you are trying to set d.DiskSize as s.Projects.Select(y => y.newDiskSizeInfo...); which is an IEnumerable < DiskSizeInfo >.

The right thing to do is:

ForMember(d => d.DiskSize, o => o.MapFrom(s => new DiskSizeInfo
                {
                    Projects = s.Projects.Select(y => new
                        {
                            Name = y.Name,
                            Used = y.Servers.Sum(z => z.DiskSize)
                        }).ToList(),
                    Total = s.Projects.Sum(x => x.Servers.Sum(y => y.DiskSize)) //I assumed you wanted to sum the DiskSize of the Servers of each project
                }))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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