[英]How to map Object to KeyValuePair<char, string> using AutoMapper?
I'm trying to map my object model to a KeyValuePair but my results are KeyValuePairs where Key = null and Value = null.我试图将我的对象模型映射到 KeyValuePair,但我的结果是 KeyValuePairs,其中 Key = null 和 Value = null。
What's the correct way to do this?这样做的正确方法是什么?
Thanks!谢谢!
Asp.Net Core 3.1 Asp.Net 核心 3.1
AutoMapper 9.0.0自动映射器 9.0.0
AutoMapper.Extensions.Microsoft.DependencyInjection 7.0.0 AutoMapper.Extensions.Microsoft.DependencyInjection 7.0.0
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;
}
Solved by using the following :使用以下方法解决:
CreateMap<Symbol, KeyValuePair<char, string>>()
.ConstructUsing(sym => new KeyValuePair<char, string>(sym.Letter, sym.ImgSource));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.