0

I have 3 models in my ASP.Net core project

public class Product : BaseModel
{
    [Required(ErrorMessage = "Product name is a required field.")]
    [MaxLength(60, ErrorMessage = "Maximum length for the Name is 50 characters")]
    public string Name { get; set; }
    [Required(ErrorMessage = "Product DefaultQuantity is a required field.")]
    public int? DefaultQuantity { get; set; }

    public IList<FridgeProduct> FridgeProducts { get; set; }
}

public class FridgeProduct : BaseModel
{
    public Fridge Fridge { get; set; }
    public Guid FridgeId { get; set; }

    public Product Product { get; set; }
    public Guid ProductId { get; set; }

    public int Quantity { get; set; }

}

public class ProductDto : BaseModel
{
    public string Name { get; set; }
    public int Quantity { get; set; }
}

How i can map Product to ProductDto and take property "Quantity" from FridgeProduct?

What i try to do but its doesnt work

public MappingProfile()
    {
        
        CreateMap<Product, ProductDto>()
            .ForMember(p => p.Quantity,
            opt => opt.MapFrom(src => src.FridgeProducts.Select(s => s.Quantity)));

    }
  • I think this is a duplicate of: https://stackoverflow.com/questions/21413273/automapper-convert-from-multiple-sources – Samuel Wahlberg May 27 '22 at 12:18
  • Would be great if you can explain and prvodie requirement for what Quantity you want to get in the `FridgeProduct` for `FridgeProduct`. Take the quantity from first/last item, sum of the quantity for all items? – Yong Shun May 28 '22 at 10:58

0 Answers0