0

I'm trying to map my object model to a KeyValuePair<char, string> but my results are KeyValuePairs where Key = null and Value = null.

What's the correct way to do this?


Model:

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

Profile:

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

Attempt:

                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;
                }
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
roonz11
  • 795
  • 1
  • 13
  • 24

1 Answers1

0

Solved by using the following :

CreateMap<Symbol, KeyValuePair<char, string>>()
                .ConstructUsing(sym => new KeyValuePair<char, string>(sym.Letter, sym.ImgSource));
roonz11
  • 795
  • 1
  • 13
  • 24