简体   繁体   中英

C# Automapper Mapping Dictionary Property

I'm struggling to figure out how to map a value based on key from Dictionary object.

Here is what I have so far:

.ForMember(dest => dest.Submitter,
            opts => opts.MapFrom(src => src.opened_by))

Note: src.open_by is a dictionary object. I would like to search by key to get value to map to dest.Submitter

Additional info:

Here is the destination object:

public class Incident
{
    public int Active { get; set; }
    public string Submitter { get; set; }
}

Here is the source object:

Dictionary<string, string> opened_by = new Dictionary<string, string>
{
    { "link", "https://someurl/674bd1f96f03e10071c35e02be3ee4ae" },
    { "value", "674bd1f96f03e10071c35e02be3ee4ae" }
};

My goal is to get the the value 674bd1f96f03e10071c35e02be3ee4ae from the "value" key to hydrate the "Submitter" property of the Incident object

Thanks! :)

You should be able to get the mapper to set the specific value from the Dictionary like this:

.ForMember(dest => dest.Submitter,
        opts => opts.MapFrom(src => src.opened_by["value"]))

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