Как сопоставить объект с KeyValuePair используя AutoMapper? - PullRequest
0 голосов
/ 31 марта 2020

Я пытаюсь сопоставить мою объектную модель с KeyValuePair, но мои результаты - KeyValuePairs, где Key = null и Value = null.

Какой правильный способ сделать это?

Спасибо !


Asp. Net Core 3.1

AutoMapper 9.0.0

AutoMapper.Extensions.Microsoft.DependencyInjection 7.0.0


Модель:

public class Symbol
    {
        public int Id { get; set; }
        public int TemplateId { get; set; }
        public Template Template { get; set; }
        public char Letter { get; set; }
        public string ImgSource { get; set; }
    }

Профиль:

    public class AutoMapping : Profile
    {
        public AutoMapping()
        {
            CreateMap<Symbol, KeyValuePair<object, object>>()
                      .ForMember(dest => dest.Key, opt => opt.MapFrom(src => src.Letter))
                      .ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.ImgSource))
                      .ReverseMap();

        }
    }

Попытка:

                using (_dbContext)
                {
                    var q = await _dbContext.Symbol
                        .Where(x => x.TemplateId == templateId)
                        .OrderBy(x => x.Letter)
                        .Select(x => _mapper.Map<KeyValuePair<char, string>>(x))
                        .ToListAsync();

                    //return _mapper.Map<List<KeyValuePair<char, string>>>(q);
                    return q;
                }

1 Ответ

0 голосов
/ 31 марта 2020

Решено с помощью следующего:

CreateMap<Symbol, KeyValuePair<char, string>>()
                .ConstructUsing(sym => new KeyValuePair<char, string>(sym.Letter, sym.ImgSource));
...