为什么在 aspnet.core 3.1 中映射 List<T> 时 Automapper returns 为空?

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

在我的 .net core 3.1 中,我想使用 automapper 显示嵌套元素。

在我的 dto 中有字段:

    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()))

想要这样的结果:

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

您返回的类型不兼容:

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 具有类型 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()))

在前面的代码中,您试图将 d.DiskSize 设置为 s.Projects.Select(y => y.newDiskSizeInfo...);,这是一个 IEnumerable < DiskSizeInfo >.

正确的做法是:

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
                }))