4
votes

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! :)

1
What kind of dictionary object? What are the keys and values? What do the source and destination types look like? - Andrew Whitaker
Sorry about that.. I just added some more info. - jaypman
No worries, thanks for updating - Andrew Whitaker

1 Answers

4
votes

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"]))