简体   繁体   中英

Automapper get source value from a Dictionary<string, string>

I am trying to map from an item in a dictionary.

class Dest
{
    Dictionary<string, string> dict { get; set; } = new Dictionary<string, string>();
}

class Src
{
    public string src { get; set }
}

mapper.CreateMap<Src, Dest>()
    .ForMember(m => m.src, opt.MapFrom(s => s.dict.SingleOrDefault(i => i.Key.Equals("id"))));

The item in the dictionary definitely exists but the value is not being mapped.

Any obvious mistakes?

Argh. So it turns out my Dest class also has a property called src . It seems mapper is doing my optional map and then also doing a default src to src map.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM