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)));
}