0

I am currently using Automapper 8.0 and trying to pass in a parameter to the mapper as a Condition for a map.

I'm mapping two object that contain an IEnumerable of another object. For example...

public class Source {
   public IEnumerable<SourceIE> value {get; set;}
}

public class SourceIE
{
   public long Id { get; set; }
   public string Name { get; set; }
   public number Rating { get; set; }
}

public class Destination {
   public <IEnumerable>DestinationIE value {get; set;}
}

public class DestinationIE
{
   public long ID { get; set; }
   public string Name { get; set; }
   public number Rating { get; set; }
}

I set up the automapper for my root object to ignore my subdocument and then created a new mapper for my subdocuments which I call like:

    var mapped = _mapper.Map<IEnumerable<Destination>>
        (data, opt => opt.Items.Add("ShowStarRating", false));

and inside the mapper, I'm trying to set a condition so that Rating will only be mapped if ShowStarRating = true.

.ForMember(dest => dest.Rating, opt =>
    {
    opt.Condition(context=>
        {
            return (bool)context.Options.Items["ShowStarRating"];
        }
    );
    opt.MapFrom(src => src.Rating);
    }
);

This issue i'm having is that Options is not recognized and when I hover it says that 'Source' does not contain a definition for 'Options' and no accessible extension method.

I can't figure out for the life of me how to access the passed in Options.Items values from inside the Condition. There seems to be a ton of information on how to do it in Automapper 5 or lower, but nothing for 8.

Peej
  • 3
  • 1

1 Answers1

0

After a bunch of trial and error I was able to figure this one out. Turns out context was the 4th item in conditions so I just needed to get to it before I could read it.

.ForMember(dest => dest.Rating, 
    opt => opt.Condition(
        (src, dest, x, y, context) => context.Options.Items["ShowStarRating"] == "true"
    )
);                
Peej
  • 3
  • 1