jaypman
jaypman

Reputation: 167

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

Upvotes: 4

Views: 2383

Answers (1)

Marc Harry
Marc Harry

Reputation: 2430

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

Upvotes: 5

Related Questions