Почему Automapper возвращает null при сопоставлении List <T>с pnet .core 3.1? - PullRequest
0 голосов
/ 13 июля 2020

В моем. 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
  }

1 Ответ

2 голосов
/ 13 июля 2020

вы возвращаете несовместимые типы:

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. Выберите (y => y.newDiskSizeInfo ...); который является IEnumerable .

Правильно:

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