如何使用AutoMapper将对象映射到KeyValuePair <char,string>?

我正在尝试将对象模型映射到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;
                }
book1842 回答:如何使用AutoMapper将对象映射到KeyValuePair <char,string>?

使用以下方法解决:

CreateMap<Symbol,KeyValuePair<char,string>>()
                .ConstructUsing(sym => new KeyValuePair<char,string>(sym.Letter,sym.ImgSource));
本文链接:https://www.f2er.com/2546252.html

大家都在问