1

I am using profiles and looking for the best approach on the following, what I have in my profile is not working.

public EbayItemProfile()
        {
            CreateMap<Item, EbayItem>()
                .ForMember(dest => dest.Availability.ShipToLocationAvailability.Quantity, opt => opt.MapFrom(src => src.Quantity))
                .ForMember(dest => dest.Sku, opt => opt.MapFrom(src => src.Sku))
                .ForMember(dest => dest.Product.Title, opt => opt.MapFrom(src => src.Name));
        }

Here are some example object setups which I am trying to map

var items = new List<Item>()
            {
                new Item()
                {
                    Quantity = "2",
                    Sku = "Test-Sku-2",
                    Name = "Test-item-2"
                }
            };

to ->

 var expected = new List<EbayItem>()
    {
        new EbayItem()
        {
            Availability = new Availability()
            {
                ShipToLocationAvailability = new ShipToLocationAvailability()
                {
                    Quantity = "2"
                }
            },
            Sku = "Test-Sku-2",
            Product = new Product()
            {
                Title = "Test-item-2"
            }
        }};

Can anyone help or advise here? .NET 6.0, Automapper 11.0.0

1
  • What is your error message? Commented Jan 7, 2022 at 9:59

1 Answer 1

2

You can use the .ForPath() method. I have created a mapping profile and this should work in your use-case.

CreateMap<Item, EbayItem>()
            .ForPath(dest => dest.Availability.ShipToLocationAvailability.Quantity, opt => opt.MapFrom(src => src.Quantity))
            .ForMember(dest => dest.Sku, opt => opt.MapFrom(src => src.Sku))
            .ForPath(dest => dest.Product.Title, opt => opt.MapFrom(src => src.Name));
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect thank you much appreciated! :)
How would you so it if you there is a property which is IEnumerable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.